草庐IT

variable-initialization

全部标签

c++ - 如何 move initializer_list 的元素?

假设您有一个std::vector类型的变量并使用初始化列表对其进行初始化:usingV=std::vector;Vv={"Hello","little","world","of","move","semantics"};编译器会创建一个临时的std::string对于每个字符串文字,创建一个初始化列表,然后调用V的ctor并创建vector。ctor不知道所有这些字符串都是临时的,所以它复制每个字符串。我没有在标准中找到任何允许vectorctor在临时元素时move它们的内容。我是否遗漏了什么或者使用初始化列表会导致不必要的拷贝?我正在编写这个问题可能导致代码效率显着低下的类。任何

C++ 模板 : The static member in a global object is not initialized

我有一段简单的C++代码,其中我通过专门化模板定义了一个模板和一个全局对象。对象构造函数访问专用模板中的静态成员。但事实证明,此时静态成员并未初始化。但是对于本地对象(在函数体中定义),它可以工作。我很困惑...我的c++编译器是:g++(Ubuntu5.4.0-6ubuntu1~16.04.4)5.4.020160609/////////////////////////templateclassTB{public:constchar*_name;TB(constchar*str):_name(str){coutclassTA{public:constchar*_name;TA(con

C++ 模板 : The static member in a global object is not initialized

我有一段简单的C++代码,其中我通过专门化模板定义了一个模板和一个全局对象。对象构造函数访问专用模板中的静态成员。但事实证明,此时静态成员并未初始化。但是对于本地对象(在函数体中定义),它可以工作。我很困惑...我的c++编译器是:g++(Ubuntu5.4.0-6ubuntu1~16.04.4)5.4.020160609/////////////////////////templateclassTB{public:constchar*_name;TB(constchar*str):_name(str){coutclassTA{public:constchar*_name;TA(con

c++ - 错误 : invalid initialization of reference of type 'int&' from expression of type 'const int'

这是一个与thisquestion中的代码无关的问题。,关于以下模板函数。templateclassObject:publicContainer{public:T&object;Object(constT&obj):object(obj){}};这是调用构造函数的代码:templatevoidArray::add_element(constT&element){vec.push_back(newObject(element));}这段代码编译得很好,但是只要我在main中添加一行调用它:Arrayarray;inti=3;array.add_element(i);我收到编译器警告:er

c++ - 错误 : invalid initialization of reference of type 'int&' from expression of type 'const int'

这是一个与thisquestion中的代码无关的问题。,关于以下模板函数。templateclassObject:publicContainer{public:T&object;Object(constT&obj):object(obj){}};这是调用构造函数的代码:templatevoidArray::add_element(constT&element){vec.push_back(newObject(element));}这段代码编译得很好,但是只要我在main中添加一行调用它:Arrayarray;inti=3;array.add_element(i);我收到编译器警告:er

C++ 警告 : anonymous type with no linkage used to declare variable

编译(gcc4.6.3,ubuntu)示例时看到此警告消息:struct{}a;intmain(){}warning:anonymoustypewithnolinkageusedtodeclarevariable‘a’withlinkage[enabledbydefault].GCC没有给出这个警告。只有G++可以。添加static清除警告:staticstruct{}a;我不明白它是什么意思,特别是为什么type与linkage相关。我认为链接取决于变量的声明位置和方式,而不取决于变量本身的类型。 最佳答案 这意味着变量a具有链接

C++ 警告 : anonymous type with no linkage used to declare variable

编译(gcc4.6.3,ubuntu)示例时看到此警告消息:struct{}a;intmain(){}warning:anonymoustypewithnolinkageusedtodeclarevariable‘a’withlinkage[enabledbydefault].GCC没有给出这个警告。只有G++可以。添加static清除警告:staticstruct{}a;我不明白它是什么意思,特别是为什么type与linkage相关。我认为链接取决于变量的声明位置和方式,而不取决于变量本身的类型。 最佳答案 这意味着变量a具有链接

c++ - 如何使用 condition_variable 来真正 wait_for 不超过一定的持续时间

作为itturnsout,condition_variable::wait_for真的应该叫condition_variable::wait_for_or_possibly_indefinitely_longer_than,因为它需要在真正超时和返回之前重新获取锁。见thisprogram进行演示。有没有办法表达,“看,我真的只有2秒。如果当时myPredicate()仍然是假的和/或锁是仍然被锁定,我不在乎,只要继续,给我一个方法来检测它。”类似:boolmyPredicate();autosec=std::chrono::seconds(1);boolpred;std::condi

c++ - 如何使用 condition_variable 来真正 wait_for 不超过一定的持续时间

作为itturnsout,condition_variable::wait_for真的应该叫condition_variable::wait_for_or_possibly_indefinitely_longer_than,因为它需要在真正超时和返回之前重新获取锁。见thisprogram进行演示。有没有办法表达,“看,我真的只有2秒。如果当时myPredicate()仍然是假的和/或锁是仍然被锁定,我不在乎,只要继续,给我一个方法来检测它。”类似:boolmyPredicate();autosec=std::chrono::seconds(1);boolpred;std::condi

c++ - 无法将 {...} 从 <brace-enclosed initializer list> 转换为 struct

我以前使用过TDM-GCC-5.10,现在切换回4.9MINGW-GCC,尝试使用列表初始化时遇到了奇怪的错误:classVector2{public:Vector2(floatx,floaty){this->x=x;this->y=y;}floatx=0.f;floaty=0.f;};structTest{intx=0;Vector2v;};intmain(){Testtst={0,Vector2(0.0f,0.0f)};//Errorreturn0;}错误:main.cpp:Infunction'intmain()':main.cpp:21:41:error:couldnotcon