Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用C+中的RAII移动线程包装器类中的语义+; 我试图在STD::线程在C++中工作。 #include <iostream> #include <thread> #include <vector> using std::cout; using std::endl; void threadFunction() { for (int i = 0; i < 20; ++i) { cout << "thread function pointer executing..." << i << endl; } } // RAII wrapper that supports automatic join class ThreadJoinRAII { std::thread thread_; public: ThreadJoinRAII(std::thread&& t):thread_(std::move(t)) // ThreadJoinRAII() { cout << "ThreadJoinRAII ctor" << endl; } //ThreadJoinRAII(const ThreadJoinRAII& other) = delete; //ThreadJoinRAII& operator=(const ThreadJoinRAII& other) = delete; //ThreadJoinRAII(ThreadJoinRAII&& other):thread_(std::move(other.thread_)) //{ // cout << "ThreadJoinRAII move ctor" << endl; //} //ThreadJoinRAII& operator=(ThreadJoinRAII&& other) //{ // cout << "ThreadJoinRAII move op" << endl; // thread_ = std::move(other.thread_); // return *this; //} ~ThreadJoinRAII() { cout << "in ThreadJoinRAII dtor" << endl; if (thread_.joinable()) { thread_.join(); cout << "join thread id = " << thread_.get_id() << endl; } else { cout << "thread not joinable. id=" << thread_.get_id() << endl; } } }; class Something { std::vector<int> vec_; public: Something(std::vector<int>&& v):vec_(std::move(v)) { cout << "Something ctor" << endl; } }; void testRAII() { ThreadJoinRAII t1(std::move(std::thread(threadFunction))); // prints ThreadJoinRAII ctor ThreadJoinRAII t2(std::thread(threadFunction)); // prints nothing Something s1(std::vector<int>(3, 4)); // prints Something ctor } int main(int argc, char* argv[]) { testRAII(); return 0; } #包括 #包括 #包括 使用std::cout; 使用std::endl; void threadFunction() { 对于(int i=0;iTimer>)->TimeKeeper这很有意义。所以在这种情况下,我猜它是(threadFunction->std::thread)->ThreadJoinRAII。threadFunction怎么可能是一个类型?@dhuthreadFunction不是一个类型。它是参数名(就像ThreadJoinRAII t2(std::threadFunction);)。wiki示例没有参数名。@1201程序名,那么在本例中,参数名的类型是什么?threadFunction?我相信我遇到了一个最麻烦的解析案例,但我不认为我完全理解了原因和方式。@dhuthreadFunction属于std::thread类型。这与任何函数一样旋转类型或声明:参数声明由类型和可选参数名称组成。或者使用大括号版本:ThreadJoinRAII t2{std::thread(threadFunction)};谢谢。这是维基百科TimeKeeper time_keeper(Timer());中的示例,它被中断为(void->Timer)->TimeKeeper这很有意义。所以在这种情况下,我猜它是(threadFunction->std::thread)->ThreadJoinRAII。threadFunction怎么可能是一个类型?@dhuthreadFunction不是一个类型。它是参数名(就像ThreadJoinRAII t2(std::threadFunction);)。wiki示例没有参数名。@1201程序名,那么在本例中,参数名的类型是什么?threadFunction?我相信我遇到了一个最麻烦的解析案例,但我不认为我完全理解了原因和方式。@dhuthreadFunction属于std::thread类型。这与任何函数一样rototype或声明:参数声明由一个类型和可选参数名组成。与您的问题无关,但您应该将单参数构造函数标记为explicit@AlanBirtles这是一般性建议吗?我知道我遇到了explicit保存我的情况。例如,当我定义运算符时r bool()const函数。是的,它并不总是必要的,但通常不会造成任何伤害,因此养成这样的习惯是一个好习惯。RAII类型类尤其重要,因为意外转换将导致包含的类被破坏,与您的问题无关,但您应该将单参数构造函数标记为显式@AlanBirtl这是一个通用的建议吗?我知道我遇到过显式保存我的情况。例如,当我定义操作符bool()时const函数。是的,它并不总是必要的,但通常不会造成任何伤害,因此养成一个好习惯。RAII类型类尤其重要,因为意外转换会导致包含的类被破坏_C++_Multithreading_Move_Most Vexing Parse - Fatal编程技术网

使用C+中的RAII移动线程包装器类中的语义+; 我试图在STD::线程在C++中工作。 #include <iostream> #include <thread> #include <vector> using std::cout; using std::endl; void threadFunction() { for (int i = 0; i < 20; ++i) { cout << "thread function pointer executing..." << i << endl; } } // RAII wrapper that supports automatic join class ThreadJoinRAII { std::thread thread_; public: ThreadJoinRAII(std::thread&& t):thread_(std::move(t)) // ThreadJoinRAII() { cout << "ThreadJoinRAII ctor" << endl; } //ThreadJoinRAII(const ThreadJoinRAII& other) = delete; //ThreadJoinRAII& operator=(const ThreadJoinRAII& other) = delete; //ThreadJoinRAII(ThreadJoinRAII&& other):thread_(std::move(other.thread_)) //{ // cout << "ThreadJoinRAII move ctor" << endl; //} //ThreadJoinRAII& operator=(ThreadJoinRAII&& other) //{ // cout << "ThreadJoinRAII move op" << endl; // thread_ = std::move(other.thread_); // return *this; //} ~ThreadJoinRAII() { cout << "in ThreadJoinRAII dtor" << endl; if (thread_.joinable()) { thread_.join(); cout << "join thread id = " << thread_.get_id() << endl; } else { cout << "thread not joinable. id=" << thread_.get_id() << endl; } } }; class Something { std::vector<int> vec_; public: Something(std::vector<int>&& v):vec_(std::move(v)) { cout << "Something ctor" << endl; } }; void testRAII() { ThreadJoinRAII t1(std::move(std::thread(threadFunction))); // prints ThreadJoinRAII ctor ThreadJoinRAII t2(std::thread(threadFunction)); // prints nothing Something s1(std::vector<int>(3, 4)); // prints Something ctor } int main(int argc, char* argv[]) { testRAII(); return 0; } #包括 #包括 #包括 使用std::cout; 使用std::endl; void threadFunction() { 对于(int i=0;iTimer>)->TimeKeeper这很有意义。所以在这种情况下,我猜它是(threadFunction->std::thread)->ThreadJoinRAII。threadFunction怎么可能是一个类型?@dhuthreadFunction不是一个类型。它是参数名(就像ThreadJoinRAII t2(std::threadFunction);)。wiki示例没有参数名。@1201程序名,那么在本例中,参数名的类型是什么?threadFunction?我相信我遇到了一个最麻烦的解析案例,但我不认为我完全理解了原因和方式。@dhuthreadFunction属于std::thread类型。这与任何函数一样旋转类型或声明:参数声明由类型和可选参数名称组成。或者使用大括号版本:ThreadJoinRAII t2{std::thread(threadFunction)};谢谢。这是维基百科TimeKeeper time_keeper(Timer());中的示例,它被中断为(void->Timer)->TimeKeeper这很有意义。所以在这种情况下,我猜它是(threadFunction->std::thread)->ThreadJoinRAII。threadFunction怎么可能是一个类型?@dhuthreadFunction不是一个类型。它是参数名(就像ThreadJoinRAII t2(std::threadFunction);)。wiki示例没有参数名。@1201程序名,那么在本例中,参数名的类型是什么?threadFunction?我相信我遇到了一个最麻烦的解析案例,但我不认为我完全理解了原因和方式。@dhuthreadFunction属于std::thread类型。这与任何函数一样rototype或声明:参数声明由一个类型和可选参数名组成。与您的问题无关,但您应该将单参数构造函数标记为explicit@AlanBirtles这是一般性建议吗?我知道我遇到了explicit保存我的情况。例如,当我定义运算符时r bool()const函数。是的,它并不总是必要的,但通常不会造成任何伤害,因此养成这样的习惯是一个好习惯。RAII类型类尤其重要,因为意外转换将导致包含的类被破坏,与您的问题无关,但您应该将单参数构造函数标记为显式@AlanBirtl这是一个通用的建议吗?我知道我遇到过显式保存我的情况。例如,当我定义操作符bool()时const函数。是的,它并不总是必要的,但通常不会造成任何伤害,因此养成一个好习惯。RAII类型类尤其重要,因为意外转换会导致包含的类被破坏

使用C+中的RAII移动线程包装器类中的语义+; 我试图在STD::线程在C++中工作。 #include <iostream> #include <thread> #include <vector> using std::cout; using std::endl; void threadFunction() { for (int i = 0; i < 20; ++i) { cout << "thread function pointer executing..." << i << endl; } } // RAII wrapper that supports automatic join class ThreadJoinRAII { std::thread thread_; public: ThreadJoinRAII(std::thread&& t):thread_(std::move(t)) // ThreadJoinRAII() { cout << "ThreadJoinRAII ctor" << endl; } //ThreadJoinRAII(const ThreadJoinRAII& other) = delete; //ThreadJoinRAII& operator=(const ThreadJoinRAII& other) = delete; //ThreadJoinRAII(ThreadJoinRAII&& other):thread_(std::move(other.thread_)) //{ // cout << "ThreadJoinRAII move ctor" << endl; //} //ThreadJoinRAII& operator=(ThreadJoinRAII&& other) //{ // cout << "ThreadJoinRAII move op" << endl; // thread_ = std::move(other.thread_); // return *this; //} ~ThreadJoinRAII() { cout << "in ThreadJoinRAII dtor" << endl; if (thread_.joinable()) { thread_.join(); cout << "join thread id = " << thread_.get_id() << endl; } else { cout << "thread not joinable. id=" << thread_.get_id() << endl; } } }; class Something { std::vector<int> vec_; public: Something(std::vector<int>&& v):vec_(std::move(v)) { cout << "Something ctor" << endl; } }; void testRAII() { ThreadJoinRAII t1(std::move(std::thread(threadFunction))); // prints ThreadJoinRAII ctor ThreadJoinRAII t2(std::thread(threadFunction)); // prints nothing Something s1(std::vector<int>(3, 4)); // prints Something ctor } int main(int argc, char* argv[]) { testRAII(); return 0; } #包括 #包括 #包括 使用std::cout; 使用std::endl; void threadFunction() { 对于(int i=0;iTimer>)->TimeKeeper这很有意义。所以在这种情况下,我猜它是(threadFunction->std::thread)->ThreadJoinRAII。threadFunction怎么可能是一个类型?@dhuthreadFunction不是一个类型。它是参数名(就像ThreadJoinRAII t2(std::threadFunction);)。wiki示例没有参数名。@1201程序名,那么在本例中,参数名的类型是什么?threadFunction?我相信我遇到了一个最麻烦的解析案例,但我不认为我完全理解了原因和方式。@dhuthreadFunction属于std::thread类型。这与任何函数一样旋转类型或声明:参数声明由类型和可选参数名称组成。或者使用大括号版本:ThreadJoinRAII t2{std::thread(threadFunction)};谢谢。这是维基百科TimeKeeper time_keeper(Timer());中的示例,它被中断为(void->Timer)->TimeKeeper这很有意义。所以在这种情况下,我猜它是(threadFunction->std::thread)->ThreadJoinRAII。threadFunction怎么可能是一个类型?@dhuthreadFunction不是一个类型。它是参数名(就像ThreadJoinRAII t2(std::threadFunction);)。wiki示例没有参数名。@1201程序名,那么在本例中,参数名的类型是什么?threadFunction?我相信我遇到了一个最麻烦的解析案例,但我不认为我完全理解了原因和方式。@dhuthreadFunction属于std::thread类型。这与任何函数一样rototype或声明:参数声明由一个类型和可选参数名组成。与您的问题无关,但您应该将单参数构造函数标记为explicit@AlanBirtles这是一般性建议吗?我知道我遇到了explicit保存我的情况。例如,当我定义运算符时r bool()const函数。是的,它并不总是必要的,但通常不会造成任何伤害,因此养成这样的习惯是一个好习惯。RAII类型类尤其重要,因为意外转换将导致包含的类被破坏,与您的问题无关,但您应该将单参数构造函数标记为显式@AlanBirtl这是一个通用的建议吗?我知道我遇到过显式保存我的情况。例如,当我定义操作符bool()时const函数。是的,它并不总是必要的,但通常不会造成任何伤害,因此养成一个好习惯。RAII类型类尤其重要,因为意外转换会导致包含的类被破坏,c++,multithreading,move,most-vexing-parse,C++,Multithreading,Move,Most Vexing Parse,乍一看可能不太像,但是t2是一个函数原型,而不是一个变量声明(搜索“最麻烦的解析”) 可以通过添加一些括号将其更改为变量: ThreadJoinRAII t2((std::thread(threadFunction))); 然后它将调用您的ThreadJoinRAII构造函数。乍看起来可能不像它,但t2是一个函数原型,而不是一个变量声明(搜索“最麻烦的解析”) 可以通过添加一些括号将其更改为变量: ThreadJoinRAII t2((std::thread(threadFunction)))

乍一看可能不太像,但是
t2
是一个函数原型,而不是一个变量声明(搜索“最麻烦的解析”)

可以通过添加一些括号将其更改为变量:

ThreadJoinRAII t2((std::thread(threadFunction)));

然后它将调用您的
ThreadJoinRAII
构造函数。

乍看起来可能不像它,但
t2
是一个函数原型,而不是一个变量声明(搜索“最麻烦的解析”)

可以通过添加一些括号将其更改为变量:

ThreadJoinRAII t2((std::thread(threadFunction)));

然后它将调用您的
ThreadJoinRAII
构造函数。

或使用大括号版本:
ThreadJoinRAII t2{std::thread(threadFunction)};
谢谢。这是维基百科
TimeKeeper time_keeper(Timer());
中的示例,它被中断为
(void->Timer>)->TimeKeeper
这很有意义。所以在这种情况下,我猜它是
(threadFunction->std::thread)->ThreadJoinRAII
threadFunction
怎么可能是一个类型?@dhu
threadFunction
不是一个类型。它是参数名(就像
ThreadJoinRAII t2(std::threadFunction);
)。wiki示例没有参数名。@1201程序名,那么在本例中,参数名的类型是什么?
threadFunction
?我相信我遇到了一个最麻烦的解析案例,但我不认为我完全理解了原因和方式。@dhu
threadFunction
属于
std::thread
类型。这与任何函数一样旋转类型或声明:参数声明由类型和可选参数名称组成。或者使用大括号版本:
ThreadJoinRAII t2{std::thread(threadFunction)};
谢谢。这是维基百科
TimeKeeper time_keeper(Timer());
中的示例,它被中断为
(void->Timer)->TimeKeeper
这很有意义。所以在这种情况下,我猜它是
(threadFunction->std::thread)->ThreadJoinRAII
threadFunction
怎么可能是一个类型?@dhu
threadFunction
不是一个类型。它是参数名(就像
ThreadJoinRAII t2(std::threadFunction);
)。wiki示例没有参数名。@1201程序名,那么在本例中,参数名的类型是什么?
threadFunction
?我相信我遇到了一个最麻烦的解析案例,但我不认为我完全理解了原因和方式。@dhu
threadFunction
属于
std::thread
类型。这与任何函数一样rototype或声明:参数声明由一个类型和可选参数名组成。与您的问题无关,但您应该将单参数构造函数标记为
explicit
@AlanBirtles这是一般性建议吗?我知道我遇到了explicit保存我的情况。例如,当我定义
运算符时r bool()const
函数。是的,它并不总是必要的,但通常不会造成任何伤害,因此养成这样的习惯是一个好习惯。RAII类型类尤其重要,因为意外转换将导致包含的类被破坏,与您的问题无关,但您应该将单参数构造函数标记为
显式
@AlanBirtl这是一个通用的建议吗?我知道我遇到过显式保存我的情况。例如,当我定义
操作符bool()时const
函数。是的,它并不总是必要的,但通常不会造成任何伤害,因此养成一个好习惯。RAII类型类尤其重要,因为意外转换会导致包含的类被破坏