jjzjj

explicit

全部标签

c++ - 防止模板化成员函数中某些参数的隐式转换

目前我有一个这样定义的成员函数:templateboolupdateParameter(conststd::string&name,constT&data);指针重载。templateboolupdateParameter(conststd::string&name,T*data);我希望能够像这样使用这个函数:inttest=20;updateParameter("name",0);updateParameter("Referencedparameter",&test);这样我就可以拥有一个参数对象,该对象要么拥有它所代表的数据,要么指向用户拥有的成员。现在我遇到的问题是当前设置MS

CMake 添加子目录错误 : "When specifying an out-of-tree source a binary directory must be explicitly specified"

我的目录结构如下:rootlibACMakeLists.txtClassA.cpplibBCMakeLists.txtClassB.cppsharedCodeenums.hAbstractClass.hCMake文件中如何包含sharedCode目录?这样classA(在libA中)和classB(在libB中)都可以使用enums.h和AbstractClass.h?在我尝试使用的CMakeLists.txt中:add_subdirectory(../sharedCode)但它给出了错误add_subdirectorynotgivenabinarydirectorybutthegiv

c++ - 警告 : base class ‘A’ should be explicitly initialized in the copy constructor

我有以下类结构:classA{A(){}A(constA&src){}};classB:virtualA{B():A(){}B(constB&src):A(src){}};classC:virtualA{C():A(){}C(constC&src):A(src){}};classD:virtualB,virtualC{D():B(),C(){}D(constD&src):B(src),C(src){}};这给了我警告:Incopyconstructor‘D’:warning:baseclass‘A’shouldbeexplicitlyinitializedinthecopyconstr

c++ - 显式模板特化的语法

当编译为C++98或C++11时,gcc-4.9.2和clang-3.8都接受以下内容,#includetemplatevoidf(T){printf("T\n");}templatevoidf(int){printf("int\n");}//explicitspecializationtemplatevoidf(double){printf("double\n");}//explicitspecialization--14.7.2(7)templatevoidf(float){printf("float\n");}//HEREintmain(){f(1L);//Tf(10);//in

c++ - 在定义之前显式实例化模板函数

如果先声明,是否允许在定义函数之前显式实例化模板函数?例如,是否允许以下​​内容://declarationtemplatevoidfoo(Tparam);//explicitinstantiationtemplatevoidfoo(int);//definitionofprimarytemplatetemplatevoidfoo(Tparam){}总的来说它似乎可以编译,但当然这只是一个关于是否允许的轻微指示。 最佳答案 在[temp.explicit]中没有明确禁止它.此外,还有一个明确的声明([temp.explicit#5]

c++ - 函数模板显式特化 C++

我的书提到了两种显式特化的方法:templatevoidSwap(int&,int&);templatevoidSwap(int&,int&);两者有什么区别?什么时候用一个,什么时候用另一个?函数名后面的到底是什么? 最佳答案 whatisthedifferencebetweenboth?没有区别。在第二种情况下,您让编译器从特化的签名中执行类型推导。因此,两种形式都声明了Swap()的特化。对于T=int.whentouseoneandwhentousetheother?由您自行决定,当一种形式或另一种形式满足您在可读性或易于维

C++ 隐式和显式继承构造函数调用

我有一个关于对基本构造函数的隐式和显式调用的问题。如果我们有这样的类层次结构:classPerson{protected:std::stringm_name;public:Person(std::string&_name):m_name(_name){std::cout根据我的讲义,主要是对'Baby'的调用,如下所示:std::stringbabyname="Robert";intnappies=5;Babybaby(babyname,nappies);导致以下情况发生:当在Baby的初始化列表中对Person进行显式调用时:Baby的初始化列表被调用,no_of_nappies被初

C++:考虑但不调用构造函数的特殊性

关于cppreferenceaboutlist-initialization在第二个意图中(用于复制列表初始化)它说:copy-list-initialization(bothexplicitandnon-explicitconstructorsareconsidered,butonlynon-explicitconstructorsmaybecalled)“考虑”构造函数和实际“调用”构造函数究竟有什么区别。为什么要考虑可能无论如何都不会调用的构造函数? 最佳答案 “considered”和“called”的区别在于,“consi

c++ - 如何检查类型是否可显式/隐式构造?

如何检查某些类型是否可从其他类型显式(或反之亦然)构造?在这种情况下是否有任何SFINAE技巧?我可以将is_explicitly_constructible写成combinationofstd::is_constructibleandstd::is_convertible:#includetemplatestructis_explicitly_constructible:std::bool_constant::value&&!std::is_convertible::value>{};但是我是否考虑了此类代码中所有可能的情况? 最佳答案

C++ 显式构造函数

我有一个包含2个构造函数的类。explicitMyClass(size_tnum);templateMyClass(TmyObj);无论何时我都想要那个MyClassobj(30);将调用第一个构造函数,以及隐式构造函数和MyClassobj=30;将调用第二个ctor。我怎样才能让它发生? 最佳答案 30是一个带符号的整数值,因此它不完全符合您的第一个构造函数的签名(因此,模板被实例化)。您可以更改显式构造函数的签名以接受int,然后Myclassobj(30);将调用显式构造函数,或者调用它30u以便您匹配显式签名。