is_constexpr_copiable
全部标签 在C++14中,由于constexpr不再是隐式const,constexpr成员函数是否可以修改类的数据成员:structmyclass{intmember;constexprmyclass(intinput):member(input){}constexprvoidf(){member=42;}//Isitallowed?}; 最佳答案 是的,我相信这个变化是从proposalN3598:constexprmemberfunctionsandimplicitconst开始的并最终成为N3652:Relaxingconstrain
在测试聚合类型时,我尝试使用boost::proto::is_aggregate来检查我创建的类型是否真正聚合。我写了这段代码:#include#includestructIsAggregate{IsAggregate&operator=(IsAggregateconst&rhs){}};intmain(){std::cout()我希望输出为真,因为聚合类型可以定义复制赋值运算符(根据此:WhatareAggregatesandPODsandhow/whyaretheyspecial?)但是输出是错误的。我还在之前的答案中使用了聚合类,它应该返回true却返回了false。这已在Boo
使用gcc(HEAD7.0.0201612)我惊讶地发现这有效:constexprlongvalue(constchar*definition){if(definition&&*definition){return*definition+value(definition+1);}return*definition;}intmain(){longl{};std::cin>>l;switch(l){casevalue("AAAA"):f1();break;casevalue("BBBB"):f2();break;default:error();break;}return0;}文字字符串"A
我正在尝试使用模板类(此处为Foo),其基本类型如下:hana::tuple,Runtime>>与Runtime一个显然不能是constepxr的类.但是类型可以用多种方式构造,这就是我使用的原因:hana::tuple,hana::type>>在编译时完成工作。所以问题基本上是如何从第一个元组类型转换为第二个元组类型。不知hana里面有没有东西那可以帮助我。或者更好的是,关于这种“转换”的一些技巧。namespacehana=boost::hana;usingnamespacehana::literals;structRuntime{std::vectordata;};templat
以下代码在GCC和Clang下编译良好,但在VisualStudio(/std:c++latest)的最新更新中停止工作:#includetemplatevoidcheck_tuple(T...types){ifconstexpr(pos>::type;}}intmain(){check_tuple(1.0,1.0);check_tuple(1.0,1.0);}在最新版本的VisualStudio(/std:c++latest)中,编译失败,元组索引越界(std::tuple_element>)。是否可以像这样使用constexpr来防止元组越界? 最佳答案
这个问题Accesstoconstexprvariableinsidelambdaexpressionwithoutcapturing回答了为什么下面示例中的ref-capture不是严格必要的。但另一方面,如果它被捕获,则会出现错误。错误似乎是由foo()的递归性质触发的。templateconstexprintbar(constT&x){//NOK//constexprintbar(Tx){//OKreturnx;}templateintfoo(constT&l){constexprautox=l()-1;autoy=[&]{returnbar(x);};//ifref-captu
我正在开发一个包含多个类的C++项目,这些类必须是单例,它们之间存在依赖关系(初始化顺序很重要)。我想出了这个解决方案:所有我想成为单例的类都有protected构造函数,例如:classMySingleton1{protected:MySingleton1();}有一个源文件singleton_factory.cpp包含一个实例化类Singletons,它派生自所有我想成为单例的类,像这样:#include"MySingleton1.hpp"#include"MySingleton2.hpp"classSingletons:publicMySingleton1,publicMySin
#includeusingnamespacestd;constexprintr=100;intmain(){constexprint&k=r;cout编译此代码会在编译时出现“错误:将‘constint’绑定(bind)到‘int&’类型的引用会丢弃限定符”。 最佳答案 编译时在int后加入constconstexprintconst&k=r;//...........^^^^^问题是constepxr隐含了const,所以当你定义rconstexprintr=100;您将constexpr定义为intconst值(还要考虑cons
我只是偶然发现了GCC和Clang之间关于显式默认的constexprctor和一些继承的以下差异......templatestructA{constexprA()=default;Tv;};structB:A{constexprB()=default;};GCC立即拒绝该代码,而Clang允许实例化这两种类型的非constexpr版本。我的猜测是Clang可能是正确的,但我不能100%确定... 最佳答案 问题归结为:是默认初始化的constexpr构造函数一些内置类型有效的非静态数据成员,如果不使用呢?tl;dr:对于非模板构
我被指派处理MFC中的一些遗留C++代码。我在各处发现的其中一件事是如下分配:structPoint{floatx,y,z;};...voidsomeFunc(void){intnumPoints=...;Point*pArray=(Point*)newBYTE[numPoints*sizeof(Point)];...//dosomestuffwithpoints...delete[]pArray;}我意识到这段代码在很多层面上都是严重错误的(C风格转换,使用new像malloc,令人困惑,等等)。我还意识到,如果Point定义了一个构造函数,它就不会被调用,而且如果定义了析构函数,d