草庐IT

language-construct

全部标签

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++ - 调用 C++ 函数时指定默认参数

假设我有这样的代码:voidf(inta=0,intb=0,intc=0){//...SomeCode...}正如您在上面的代码中明显看到的那样,参数a、b和c的默认参数值为0。现在看看我下面的主要功能:intmain(){//Hereare4waysofcallingtheabovefunction:inta=2;intb=3;intc=-1;f(a,b,c);f(a,b);f(a);f();//notetheaboveparameterscouldbechangedfortheothervariables//aswell.}现在我知道我不能跳过一个参数,让它具有默认值,因为该值将作

c++ - 调用 C++ 函数时指定默认参数

假设我有这样的代码:voidf(inta=0,intb=0,intc=0){//...SomeCode...}正如您在上面的代码中明显看到的那样,参数a、b和c的默认参数值为0。现在看看我下面的主要功能:intmain(){//Hereare4waysofcallingtheabovefunction:inta=2;intb=3;intc=-1;f(a,b,c);f(a,b);f(a);f();//notetheaboveparameterscouldbechangedfortheothervariables//aswell.}现在我知道我不能跳过一个参数,让它具有默认值,因为该值将作

c++ - 第 854 页 "The C++ Programming Language Third Edition"中的代码是否正确?

我尝试学习C++。在“TheC++ProgrammingLanguageThirdEdition”一书中,我在第854页(附录C.13.1)找到了代码:templateclassX{staticTdef_val;staticT*new_X(Ta=def_val);};templateTX::def_val(0,0);templateT*X::new_X(Ta){/*...*/}templateintX::def_val=0;templateint*X::new_X(inti){/*...*/}我修改它:templateclassX{staticTdef_val;staticT*new_

c++ - 第 854 页 "The C++ Programming Language Third Edition"中的代码是否正确?

我尝试学习C++。在“TheC++ProgrammingLanguageThirdEdition”一书中,我在第854页(附录C.13.1)找到了代码:templateclassX{staticTdef_val;staticT*new_X(Ta=def_val);};templateTX::def_val(0,0);templateT*X::new_X(Ta){/*...*/}templateintX::def_val=0;templateint*X::new_X(inti){/*...*/}我修改它:templateclassX{staticTdef_val;staticT*new_

c++ - 了解 `std::is_move_constructible`

没有移动构造函数但具有接受constT&参数的复制构造函数的类型,满足std::is_move_constructible。例如,在以下代码中:#includestructT{T(constT&){}//T(T&&)=delete;};intmain(){static_assert(std::is_move_constructible::value,"notmoveconstructible");return0;}T将没有隐式移动构造函数,因为它有一个用户定义的复制构造函数。但是,如果我们取消注释移动构造函数的显式删除,代码将不再编译。为什么是这样?我本来希望显式复制构造函数仍然满足s

c++ - 了解 `std::is_move_constructible`

没有移动构造函数但具有接受constT&参数的复制构造函数的类型,满足std::is_move_constructible。例如,在以下代码中:#includestructT{T(constT&){}//T(T&&)=delete;};intmain(){static_assert(std::is_move_constructible::value,"notmoveconstructible");return0;}T将没有隐式移动构造函数,因为它有一个用户定义的复制构造函数。但是,如果我们取消注释移动构造函数的显式删除,代码将不再编译。为什么是这样?我本来希望显式复制构造函数仍然满足s

c++ - clang 5:std::optional 实例化参数类型的 std::is_constructible 特征

当切换到c++17并用标准解决方案替换自定义std::optional解决方案时,检测到clang5的一个非常奇怪和意外的行为。出于某种原因,由于对参数类的std::is_constructible特征的错误评估,emplace()被禁用。在复制之前必须满足一些特定的先决条件:#include///Precondition#1:TmustbeanestedstructstructFoo{structVictim{///Precondition#2:Tmusthaveanaggregate-initializer///foroneofitsmembersstd::size_tvalue{

c++ - clang 5:std::optional 实例化参数类型的 std::is_constructible 特征

当切换到c++17并用标准解决方案替换自定义std::optional解决方案时,检测到clang5的一个非常奇怪和意外的行为。出于某种原因,由于对参数类的std::is_constructible特征的错误评估,emplace()被禁用。在复制之前必须满足一些特定的先决条件:#include///Precondition#1:TmustbeanestedstructstructFoo{structVictim{///Precondition#2:Tmusthaveanaggregate-initializer///foroneofitsmembersstd::size_tvalue{

c++ - is_trivially_copyable 和 is_trivially_copy_constructible 有什么区别?

这些何时会给出不同的答案,这种差异何时有用(如果有的话)? 最佳答案 前者测试triviallycopyable属性,简而言之,这意味着该类型是memcpy-安全的。Atriviallycopyableclassisaclassthat:—hasnonon-trivialcopyconstructors(12.8),—hasnonon-trivialmoveconstructors(12.8),—hasnonon-trivialcopyassignmentoperators(13.5.3,12.8),—hasnonon-trivia