C++ cout __func__
时间: 2023-10-22 13:06:52 浏览: 44
`__func__` 是一个预定义的标识符,表示当前函数或方法的名称。在 C++ 中,您可以使用 `cout` 来输出 `__func__`,以便在程序运行时查看当前函数或方法的名称。例如:
```c++
#include <iostream>
void myFunction() {
std::cout << "Current function: " << __func__ << std::endl;
}
int main() {
myFunction();
return 0;
}
```
输出将是:
```
Current function: myFunction
```
相关问题
C++ pthread_create demo
下面是一个使用 `pthread_create` 创建线程的 C++ 示例代码:
```c++
#include <iostream>
#include <pthread.h>
using namespace std;
void* thread_func(void* arg)
{
int* p = (int*)arg;
cout << "Thread ID: " << pthread_self() << ", Arg: " << *p << endl;
return NULL;
}
int main()
{
pthread_t tid;
int arg = 12345;
// 创建线程
if (pthread_create(&tid, NULL, thread_func, &arg) != 0) {
cout << "Error: Failed to create thread." << endl;
return -1;
}
// 等待线程结束
if (pthread_join(tid, NULL) != 0) {
cout << "Error: Failed to join thread." << endl;
return -1;
}
cout << "Main thread exit." << endl;
return 0;
}
```
在上面的示例代码中,我们首先定义了一个名为 `thread_func` 的函数,这个函数将作为线程的入口函数。在 `thread_func` 函数中,我们打印了当前线程的 ID 和传入的参数值,然后返回 NULL。
在主函数中,我们首先定义了一个 `pthread_t` 类型的变量 `tid`,用于存储线程 ID。然后定义了一个整型变量 `arg`,用于向线程传递参数。接着,我们使用 `pthread_create` 函数创建了一个新线程,并将线程 ID 存储在 `tid` 变量中。在调用 `pthread_create` 函数时,我们传入了四个参数:
- 第一个参数是指向线程 ID 变量的指针。
- 第二个参数是线程属性,我们将其设置为 NULL。
- 第三个参数是指向线程入口函数的指针,即 `thread_func` 函数。
- 第四个参数是传递给线程入口函数的参数,即 `arg` 变量的地址。
接下来,我们使用 `pthread_join` 函数等待线程结束。在调用 `pthread_join` 函数时,我们传入了两个参数:
- 第一个参数是要等待的线程 ID。
- 第二个参数是指向线程返回值的指针,我们将其设置为 NULL,表示不关心线程的返回值。
最后,我们在主函数中打印了一条消息,表示主线程退出。
boost的api_caller
Boost库中的api_caller是用于调用API(应用程序编程接口)的模板类。它可以将API的调用封装成一个函数调用,使得代码更加简洁和易于维护。
api_caller的使用方法如下:
1. 定义一个函数对象,用于调用API。该函数对象必须重载operator(),并且其参数和返回值必须与API的参数和返回值匹配。
2. 使用api_caller模板类创建一个对象,并将上一步中定义的函数对象传入。
3. 调用api_caller对象的operator()方法,传入API所需的参数即可完成API的调用。
以下是一个使用api_caller调用Windows API获取系统时间的示例:
```c++
#include <iostream>
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <boost/asio.hpp>
using namespace std;
typedef void(WINAPI *GetSystemTimeFunc)(LPFILETIME);
void get_system_time(FILETIME &ft)
{
HMODULE hModule = GetModuleHandle("kernel32.dll");
if (hModule != NULL)
{
GetSystemTimeFunc pFunc = (GetSystemTimeFunc)GetProcAddress(hModule, "GetSystemTimeAsFileTime");
if (pFunc != NULL)
{
pFunc(&ft);
}
}
}
int main()
{
boost::function<void(FILETIME&)> func = boost::bind(get_system_time, _1);
boost::asio::io_service io_service;
boost::asio::io_service::strand strand(io_service);
boost::asio::spawn(strand, [&func](boost::asio::yield_context yield) {
boost::asio::deadline_timer timer(yield, boost::posix_time::seconds(1));
while (true)
{
timer.async_wait(yield);
FILETIME ft;
boost::asio::spawn(strand, boost::bind(boost::asio::io_service::dispatch, &io_service, boost::bind(api_caller<void, FILETIME&>(), func, boost::ref(ft))));
SYSTEMTIME st;
FileTimeToSystemTime(&ft, &st);
cout << st.wYear << "-" << st.wMonth << "-" << st.wDay << " " << st.wHour << ":" << st.wMinute << ":" << st.wSecond << endl;
}
});
io_service.run();
return 0;
}
```
在上面的示例中,我们先定义了一个get_system_time函数,用于获取系统时间。然后使用boost::bind将该函数绑定到一个boost::function对象上,并将该对象传入api_caller模板类中。最后在程序的主循环中调用api_caller对象的operator()方法即可完成API的调用。