草庐IT

is_constexpr_copiable

全部标签

c++ - 为什么未初始化的 constexpr 变量不是常量?

我不确定这是编译器错误还是我误解了constexpr:structS{};constexprSs1{};constexprSs2;structtest{staticconstexprautot1=s1;staticconstexprautot2=s2;//errorhere};GCC4.8给我一个奇怪的错误“错误:字段初始值设定项不是常量”。s2真的不是常数吗?如果是,为什么?为了清楚起见,我实际上在我的代码中使用了一堆空结构(用于元编程https://github.com/porkybrain/Kvasir)所以我真的对这个特定示例很感兴趣。 最佳答案

c++ - 为什么 `constexpr const int &a = 1;` 在 block 范围内失败?

N45277.1.5[dcl.constexpr]p9Aconstexprspecifierusedinanobjectdeclarationdeclarestheobjectasconst.Suchanobjectshallhaveliteraltypeandshallbeinitialized.Ifitisinitializedbyaconstructorcall,thatcallshallbeaconstantexpression(5.20).Otherwise,orifaconstexprspecifierisusedinareferencedeclaration,everyf

c++ - Clang 提示未评估的上下文中未定义的 constexpr 函数

我正在使用一个简单的SFINAE技巧来检查成员函数是否存在,如下所示:#includetemplatestructhas_size{templatestaticconstexprautocheck(T*)->decltype(std::declval().size(),std::true_type{});templatestaticconstexprautocheck(...)->std::false_type;staticconstexprboolvalue=decltype(check(nullptr))::value;};//Usage:static_assert(has_siz

c++ - SFINAE : Detecting if a function is called by a compile time known value

我喜欢在我的一个ctors以编译时已知值被调用时做一些检查。有办法检测吗?所以当有人调用它时:Aa(10);因为10是编译时已知常量,所以我喜欢调用一个特殊的构造函数,如下所示:template>A(intValue){}知道如何解决这个问题吗?谢谢! 最佳答案 积分常量可以解决您的问题:structA{template*=nullptr>A(std::integral_constant){}};然后,你可以像这样使用它:Aa{std:integral_constant{}};为了便于使用,您还可以使用类似于boost::hana的

c++ - constexpr 移动构造函数是否有意义?

constexpr移动构造函数是否有意义?例如,考虑以下内容:#includeclassC{public:constexprC(std::arrayar):m_ar{ar}{}constexprC(C&&other):m_ar{std::move(other.m_ar)}{}private:std::arraym_ar;};intmain(){constexprCc1{{{1,2,3}}};constexprCc2{std::move(c1)};return0;}这不会编译,因为尽管在c1上调用了std::move,编译器推断它需要使用(隐式删除的)复制构造函数,而不是移动构造函数。我

c++ - 可以在 constexpr 上下文中使用导致未指定(不是未定义!)行为的指针的表达式吗?

根据cppreference(强调我的):Acoreconstantexpressionisanyexpressionthatdoesnothaveanyoneofthefollowinginanysubexpression(...)Anexpressionwhoseevaluationleadstoanyformofcorelanguageundefinedbehavior(includingsignedintegeroverflow,divisionbyzero,pointerarithmeticoutsidearraybounds,etc).Whetherstandardlibr

c++ - MSVC 无法评估 enable_if 中的 constexpr 函数

考虑一个简单的效用函数来计算合取,并使用这个效用来确保std::tuple中的类型都相等。#include#includeconstexprautoall()noexcept->bool{returntrue;}templateconstexprautoall(boolconstx,Bools...xs)noexcept->bool{returnx&&all(xs...);}templatestructfoo;templatestructfoo,std::enable_if_t::value...)>>{};intmain(){foo>x;}GCC和Clang可以处理这段代码,但MSV

c++ - is_assignable 和 std::unique_ptr

Here是来自gcc的测试文件,livedemostructdo_nothing{templatevoidoperator()(T*){}};intmain(){inti=0;std::unique_ptrp1(&i);std::unique_ptrp2;static_assert(!std::is_assignable::value,"");//note!here.}std::is_assignableIftheexpressionstd::declval()=std::declval()iswell-formedinunevaluatedcontext,providesthemem

c++ - 指向数据成员转换的 Constexpr 指针

GCC8.2.1和MSVC19.20编译以下代码,但Clang8.0.0和ICC19.0.1无法编译。//Baseclass.structBase{};//Dataclass.structData{intfoo;};//Derivedclass.structDerived:Base,Data{intbar;};//Mainfunction.intmain(){constexprintData::*data_p{&Data::foo};constexprintDerived::*derived_p{data_p};constexprintBase::*base_p{static_cast

c++ - 如何在 constexpr 构造函数中初始化矩阵 ONCE?

我有以下矩阵类:templateclassmatrix{floatdata[Rows][Cols];public:constexprmatrix(constfloat(&input)[Rows][Cols]):data{}{for(size_ti=0;i用法:constexprautom=matrix({{4.3f,5.0f,1.2f},{8.0f,1.9f,6.5f},{9.1f,2.2f,3.7f},});Thiscompiles(在C++20中)并且工作正常,但它需要初始化data两次。第一个data{}是必需的,因为所有内容都必须在constexpr构造函数中进行成员初始化,第