草庐IT

copy-initialization

全部标签

c++ - 什么时候 copy-and-swap 习语不适用

看完this关于我读过的copy-and-swap习语this在(2)下说:class_name&class_name::operator=(constclass_name&)(2)(2)Typicaldeclarationofacopyassignmentoperatorwhencopy-and-swapidiomcannotbeused我们什么时候应该避免使用copy-and-swap习语?什么时候它完全“不能使用”?是否存在copy-and-swap和零规则都不适用的现实案例?我确实找到了question但它过于具体,没有包括任何关于如何识别此类案例的指南。

c++ - 什么时候 copy-and-swap 习语不适用

看完this关于我读过的copy-and-swap习语this在(2)下说:class_name&class_name::operator=(constclass_name&)(2)(2)Typicaldeclarationofacopyassignmentoperatorwhencopy-and-swapidiomcannotbeused我们什么时候应该避免使用copy-and-swap习语?什么时候它完全“不能使用”?是否存在copy-and-swap和零规则都不适用的现实案例?我确实找到了question但它过于具体,没有包括任何关于如何识别此类案例的指南。

c++ - 在 vector <unique_ptr> 上使用 is_copy_constructible 误报

类型trait是否应该能够处理std::vector>之类的情况?并检测到它不是可复制的?这是https://ideone.com/gbcRUa的示例(运行g++4.8.1)#include#include#include#includeintmain(){//Thisprints1,implyingthatit'scopyconstructible,whenit'sclearlynotstd::cout>>::value如果这是is_copy_constructible的正确行为,有没有办法检测到复制结构是不正确的?好吧,不仅仅是让它无法编译。 最佳答案

c++ - 在 vector <unique_ptr> 上使用 is_copy_constructible 误报

类型trait是否应该能够处理std::vector>之类的情况?并检测到它不是可复制的?这是https://ideone.com/gbcRUa的示例(运行g++4.8.1)#include#include#include#includeintmain(){//Thisprints1,implyingthatit'scopyconstructible,whenit'sclearlynotstd::cout>>::value如果这是is_copy_constructible的正确行为,有没有办法检测到复制结构是不正确的?好吧,不仅仅是让它无法编译。 最佳答案

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

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

c++ - 处理 C++ "initialized but not referenced"警告以销毁作用域助手?

在VisualStudio中,我经常将对象仅用于RAII目的。例如:ScopeGuardclose_guard=MakeGuard(&close_file,file);close_guard的全部目的是确保文件将在函数退出时关闭,它不会在其他任何地方使用。但是,VisualStudio给我一个警告,提示“局部变量已初始化但未引用”。我想针对这种特定情况关闭此警告。你如何处理这种情况?VisualStudio认为这个对象没有用,但这是错误的,因为它有一个非平凡的析构函数。我不想为此使用#pragma警告指令,因为即使出于正当理由它也会关闭此警告。 最佳答案

c++ - 处理 C++ "initialized but not referenced"警告以销毁作用域助手?

在VisualStudio中,我经常将对象仅用于RAII目的。例如:ScopeGuardclose_guard=MakeGuard(&close_file,file);close_guard的全部目的是确保文件将在函数退出时关闭,它不会在其他任何地方使用。但是,VisualStudio给我一个警告,提示“局部变量已初始化但未引用”。我想针对这种特定情况关闭此警告。你如何处理这种情况?VisualStudio认为这个对象没有用,但这是错误的,因为它有一个非平凡的析构函数。我不想为此使用#pragma警告指令,因为即使出于正当理由它也会关闭此警告。 最佳答案

c++ - 复制初始化表单 '= {}'

鉴于以下情况:#includeclassX;classY{public:Y(){printf("1\n");}//1//operatorX();//2};classX{public:X(int){}X(constY&rhs){printf("3\n");}//3X(Y&&rhs){printf("4\n");}//4};//Y::operatorX(){printf("operatorX()-2\n");returnX{2};}intmain(){Yy{};//Calls(1)printf("j\n");Xj{y};//Calls(3)printf("k\n");Xk={y};//Ca

c++ - 复制初始化表单 '= {}'

鉴于以下情况:#includeclassX;classY{public:Y(){printf("1\n");}//1//operatorX();//2};classX{public:X(int){}X(constY&rhs){printf("3\n");}//3X(Y&&rhs){printf("4\n");}//4};//Y::operatorX(){printf("operatorX()-2\n");returnX{2};}intmain(){Yy{};//Calls(1)printf("j\n");Xj{y};//Calls(3)printf("k\n");Xk={y};//Ca