草庐IT

is_constexpr_copiable

全部标签

c++ - 不使用这个的 constexpr 成员函数?

请考虑以下两个C++14程序:程序1:structS{constexprintf()const{return42;}};Ss;intmain(){constexprintx=s.f();returnx;}程序2:structS{constexprintf()const{return42;}};intg(Ss){constexprintx=s.f();returnx;}intmain(){Ss;returng(s);}这些程序中的一个或两个都不是良构的吗?为什么/为什么不? 最佳答案 这两个程序都是良构的。C++14标准要求s.f()

c++ - 模板参数推导 : which compiler is right here?

考虑以下代码:templateclassVector{};#includetemplatevoiddoWork(constVector&,conststd::array&){}intmain(){std::arrayarr;Vectorvec;doWork(vec,arr);}在这里Vector表示在第三方库中定义的类,std::array已知其元素计数为std::size_t.我试过用clang-3.6和g++-5.1编译它。Clang毫无怨言地工作,而g++给出以下错误:test.cpp:Infunction‘intmain()’:test.cpp:17:19:error:noma

c++ - gcc 与 clang、msvc 和 icc : Is this function call ambiguous?

我能得到的所有编译器都同意这很好:templateautofoo(Check,T...)->void;templateautofoo(int,T...)->void;intmain(){foo(7,"");}但是,根据gcc,以下代码(带有不能从函数参数推导的前导模板参数)是不明确的:templateautobar(Check,T...)->void;templateautobar(int,T...)->void;intmain(){bar(7,"");//ambiguousaccordingtogccbar(7);//justfine}另一方面,clang、msvc和icc对此非常满

c++ - decltype(foo(1)) 应该实例化 constexpr 函数模板 foo 吗?

以下代码使用gcc和MSVC编译,但使用clang失败,我使用clang-3.5和当前主干进行了测试。templateconstexprautowrong=false;templateconstexprautofoo(constTt)->int{static_assert(wrong,"");return{};}usingF=decltype(foo(1));intmain(){}clang实例化函数体并偶然发现static_assert。gcc和MSVC只看函数声明,忽略函数体中的static_assert。如果删除constexpr,所有编译器都能正常编译代码。问题:如果声明了返回

c++ - 函数参数的 MSVC 和 constexpr?

这段代码用clang和gcc编译得很好。templatestructN{staticconstexprsize_tv=n;};templateconstexprbooloperator,size_tn2){returnnconstexprvoidfoo(Nv){static_assert(v{});return0;}但是,如果我使用MSVC,我得到的错误是v不是常量表达式。我能理解为什么MSVC这么认为,但我认为这是错误的,而clang/gcc是正确的。是MSVC的错误吗? 最佳答案 是的,MSVC在这里是错误的。代码格式良好似乎违

c++ - "if the context from which the specialization is referenced depends on a template parameter"是什么意思?

根据C++17标准,[temp.point]/4,强调我的,Foraclasstemplatespecialization,aclassmembertemplatespecialization,oraspecializationforaclassmemberofaclasstemplate,ifthespecializationisimplicitlyinstantiatedbecauseitisreferencedfromwithinanothertemplatespecialization,ifthecontextfromwhichthespecializationisrefere

成功解决TypeError: Object of type ‘ndarray‘ is not JSON serializable

目录成功解决TypeError:Objectoftype'ndarray'isnotJSONserializable错误原因解决方案1.使用tolist()方法2.使用astype()方法3.使用自定义Encoder结论示例代码1.使用tolist()方法2.使用astype()方法3.使用自定义Encoder成功解决TypeError:Objectoftype'ndarray'isnotJSONserializable在进行Python编程的过程中,有时候会遇到​​TypeError:Objectoftype'ndarray'isnotJSONserializable​​的错误。这个错误通常

c++ - constexpr 上下文中的 std::optional 赋值运算符

我在std::optional上摸不着头脑,根据thedocs,不应有constexpr赋值运算符。但是,当我在gcc-8.1中尝试这个片段时,它编译并工作得很好:constexprstd::optionalfoo(){std::optionalbar=3;bar=1337;returnbar;}constexprautoz=foo();有什么我想念的吗? 最佳答案 这似乎是gcc中的一个错误。我刚刚在clang-6.0中尝试过,编译失败并出现预期错误。此外,该标准没有提及赋值运算符的任何constexpr重载,因此我会将此错误报告

c++ - 使用 move ctor 的 constexpr 对象的 constexpr 数组

我有一个带有constexpr值构造函数的类,但没有复制或移动构造函数classC{public:constexprC(int){}C(constC&)=delete;C&operator=(constC&)=delete;};intmain(){constexprCarr[]={1,2};}我发现这段代码不起作用,因为它实际上是在尝试使用C的移动构造函数而不是值构造函数来就地构造。一个问题是我希望此对象不可移动(出于测试目的),但我想“好吧,好吧,我将添加一个移动构造函数。”classC{public:constexprC(int){}C(constC&)=delete;C&oper

C++14:从参数值初始化 constexpr 变量

假设我有一个类可以通过constexpr函数返回常量表达式:templatestructFoo{constexprintBar()const{returnN;}};如果我想从Foo::Bar()初始化constexpr值,我应该如何传递类型为Foo的参数?我已经尝试了这两个,每个示例中都有一个constexpr变量来测试它是否可以被初始化:templateconstexprintByValue(Foof){constexprinti=f.Bar();returnf.Bar();}templateconstexprintByReference(constFoo&f){constexpri