草庐IT

c++ - 显式初始化指针 vector 会导致转换错误?

考虑一下:std::vectorv(1,0);这在VC++10上编译得很好(即使在最大警告级别也没有警告)。但是,它不会在mac上使用llvm编译,也不会在linux上使用gcc编译,会出现类似“assigningtoint*fromincompatibletypeconstint”这样的错误。我不是在寻找解决方案——我知道第二个参数是不必要的,或者static_cast可以修复错误。我认为零可以隐式转换为任何指针类型。是什么赋予了?我可以执行以下操作:int*i=0;int*const&ii=0;constintt=0;i=t;我知道vector构造函数签名采用constT&当扩展为

c++ - 空结构和非空结构中的显式默认构造函数

仅当我将-DA=1标志传递给编译器时,以下程序才能编译:#include#include#includestructelement{element()=default;element(element&&)=default;element&operator=(element&&)=default;element(constelement&)=delete;element&operator=(constelement&)=delete;#ifAstd::vectorv;#endif};intmain(){std::vectorsource(10),destination;std::move

c++ - SFINAE enable_if 显式构造函数

我正在尝试通过enable_if在显式和隐式转换构造函数之间切换。我的代码目前看起来像#include#includeenumclassenabled{};templateusingenable_if_t=typenamestd::enable_if::type;templateusingdisable_if_t=typenamestd::enable_if::type;templatestructSStruct{staticconstexprstd::intmax_ta=A;};templatestructSCheckEnable:std::integral_constant{};t

c++ - QVector::append() 显式复制的原因是什么?

templatevoidQVector::append(constT&t){constTcopy(t);constboolisTooSmall=uint(d->size+1)>d->alloc;if(!isDetached()||isTooSmall){QArrayData::AllocationOptionsopt(isTooSmall?QArrayData::Grow:QArrayData::Default);reallocData(d->size,isTooSmall?d->size+1:d->alloc,opt);}if(QTypeInfo::isComplex)new(d->

c++ - 嵌套类显式特化 : different compiler behavior

以下代码compilesfinewithclang++6.0.0andg++7.3.0(compilationflagsare-std=c++14-Wall-Wextra-Werror-pedantic-errors)butfailstocompilewithvc++19.10.25017(compilationflagis/Za):templatestructA{templatestructB{};};templatetemplatestructA::B{staticvoidfoo();};voidA::B::foo(){}intmain(){}vc++编译错误信息:errorC29

c++ - 非命名空间范围内的显式特化在 GCC 中无法编译

以下代码在Clang中编译但在GCC中不编译:templatestructWidget{templatevoidfoo(U){}templatevoidfoo(int*){}};根据C++标准([temp.expl.spec],第2段):Anexplicitspecializationmaybedeclaredinanyscopeinwhichthecorrespondingprimarytemplatemaybedefined这是GCC中的错误吗?如果是,我如何在其错误跟踪器中找到它?这是GCC的输出:prog.cc:13:14:error:explicitspecializatio

c++ - 函数模板的显式特化导致链接器错误

函数.h:#pragmaonce#includetemplatevoidTemplatedFunction(T*p){}templatevoidTemplatedFunction(float*p){}templatevoidTemplatedFunction(char*p){}函数.cpp:#include"Functions.h"voidTest(){TemplatedFunction(NULL);TemplatedFunction(NULL);}主要.cpp:#include"Functions.h"voidTest();intmain(){Test();return0;}构建错误

c++ - 显式指定通用 lambda 的 operator() 模板参数是否合法?

以下C++代码是否符合标准?#includeintmain(){[](autov){std::cout(42);}clang++3.8.0和g++7.2.0compilethiscodefine(编译器标志为-std=c++14-Wall-Wextra-Werror-pedantic-errors)。 最佳答案 这确实符合标准。该标准指定必须有一个成员operator(),并且它在其paramater-declaration-clause中为每次出现的auto提供一个模板参数。没有禁止明确提供这些内容的措辞。行的底部:lambda的

c++ - 您可以在 C++ 中显式调用实例化的对象类构造函数吗?

创建类的实例后,我们可以显式调用构造函数吗?例如classA{A(inta){}}Ainstance;instance.A(2);我们能做到吗? 最佳答案 您可以使用placementnew,这允许new(&instance)A(2);但是,根据您的示例,您将两次调用一个对象的构造函数,这是非常糟糕的做法。相反,我建议您这样做Ainstance(2);Placementnew通常仅在需要预分配内存(例如在自定义内存管理器中)并稍后构造对象时使用。 关于c++-您可以在C++中显式调用实例

c++ - 如何显式实例化具有友元函数嵌套类的模板类 (C++)

可能之前有人问过,但这一切已经接近我对C++的理解和认知的极限,所以我在理解正在谈论的内容和到底发生了什么方面有点慢。让我直接跳到代码。这有效:templateclassFoo{structBar{Bar(){}~Bar()noexcept{}Bar(Bar&&b):Bar(){swap(*this,b);}friendvoidswap(Bar&b1,Bar&b2){/*...*/}};};templateclassFoo;//explicitinstantiationofFoowithinttype但是我该如何移动swap的定义呢?在Bar之外结构体?如果我这样做:templatec