为什么在TinyThread++; 我有一个程序,我不能使用C++ 11,我使用TyyType ++来支持线程,因为它尽可能地遵循标准的C++ API。我已经从我的程序中提取了一个最小的工作示例,尽管我不确定是否准确地捕获了问题。不管怎样,代码如下: namespace chops { template <typename Fun> struct my_thread_par { Fun f; }; template <typename Fun> void my_call(void* args) { std::cout << "here2\n"; my_thread_par<Fun> *par = (my_thread_par<Fun>*) args; par->f(); } template <typename Fun> struct my_thread { my_thread() { std::cout << "here1\n"; Fun f; my_thread_par<Fun> par; par.f = f; tthread::thread t(my_call<Fun>, (void *) &par); t.join(); } }; struct log { void operator()() { std::cout << "log\n"; } }; } int main() { chops::my_thread<chops::log> t3(); } namespace-chops{ 模板 构造我的线程{ Fun f; }; 模板 作废我的通话(作废*参数){ std::cout f(); } 模板 构造我的线程{ 我的线程(){ std::cout

为什么在TinyThread++; 我有一个程序,我不能使用C++ 11,我使用TyyType ++来支持线程,因为它尽可能地遵循标准的C++ API。我已经从我的程序中提取了一个最小的工作示例,尽管我不确定是否准确地捕获了问题。不管怎样,代码如下: namespace chops { template <typename Fun> struct my_thread_par { Fun f; }; template <typename Fun> void my_call(void* args) { std::cout << "here2\n"; my_thread_par<Fun> *par = (my_thread_par<Fun>*) args; par->f(); } template <typename Fun> struct my_thread { my_thread() { std::cout << "here1\n"; Fun f; my_thread_par<Fun> par; par.f = f; tthread::thread t(my_call<Fun>, (void *) &par); t.join(); } }; struct log { void operator()() { std::cout << "log\n"; } }; } int main() { chops::my_thread<chops::log> t3(); } namespace-chops{ 模板 构造我的线程{ Fun f; }; 模板 作废我的通话(作废*参数){ std::cout f(); } 模板 构造我的线程{ 我的线程(){ std::cout,c++,multithreading,C++,Multithreading,在这一行之后 chops::my_thread<chops::log> t3(); 您将看到功能t3未定义-砰,惊喜 您声明了一个函数,该函数不接受任何参数,并返回my_thread对象。如果要创建my_thread write的实例,请执行以下操作: chops::my_thread<chops::log> t3; chops::my_thread t3; 在这一行之后 chops::my_thread<chops::log> t3(); 您将看到功能

在这一行之后

chops::my_thread<chops::log> t3();
您将看到
功能t3未定义
-砰,惊喜

您声明了一个函数,该函数不接受任何参数,并返回
my_thread
对象。如果要创建my_thread write的实例,请执行以下操作:

chops::my_thread<chops::log> t3;
chops::my_thread t3;
在这一行之后

chops::my_thread<chops::log> t3();
您将看到
功能t3未定义
-砰,惊喜

您声明了一个函数,该函数不接受任何参数,并返回
my_thread
对象。如果要创建my_thread write的实例,请执行以下操作:

chops::my_thread<chops::log> t3;
chops::my_thread t3;

你确定这个例子可以编译吗?在
my_thread
构造函数中,你使用
par->f
,但是
par
不是指针,也没有自定义的
操作符->
它确实在编译和运行,也许我在玩它的时候更改了代码。这应该是点操作符,我正在编辑这个问题同样。你确定这个例子可以编译吗?在
my_thread
构造函数中,你使用
par->f
,但是
par
不是指针,也没有自定义的
操作符->
它肯定是编译和运行的,也许我在玩它的时候更改了代码。这应该是点操作符,我正在编辑这个问题同样。但是为什么,
chops::my_thread t3();
不也应该调用默认构造函数呢?我照你说的做了,效果很好,但我仍然不明白为什么上面的行不调用默认构造函数。@meguli对这个问题的解释-。试试
chops::my_thread t3{}
调用实例对象。但是为什么不应该调用默认构造函数呢?我的线程t3();也调用默认构造函数吗?我按照你说的做了,效果很好,但我仍然不明白为什么上面的行不调用默认构造函数。@meguli对这个问题的解释-。尝试
chops::我的线程t3{};
调用实例对象。