constexpr相关内容
我想建立一个带有constexpr-ness选项的类。而且,当然,我想利用编译时错误检查。 每个 constexpr 函数,当给定的参数不是常量表达式时,包含的构造函数必须在运行时也能工作。这就是为什么您每次在 constexpr 函数中的 static_assert 函数参数上均无法编译的原因。 因此,我读到可以使用抛出异常的机制,因为当在常量表达式上调用函数时,可以在编译时对这些异
..
例如: 对某些内容进行分类{ public: static constexpr int seconds(int小时,整数分钟,整数秒) {返回小时* 3600 +分钟* 60 +秒; } } 然后: printf(“当时看:%d\n”,某事:: seconds(10,0,0)); 将使用g ++编译为对该函数的调用,而不是放置一个常数。
..
我已经通过以下方式声明了一个类 A类 { struct B { constexpr B(uint8_t _a,uint8_t _b): a(_a), b(_b) {} bool运算符==(const B& rhs)const { if((a == rhs.a)&& (b == rhs.b)) { 返回true; } 返回false; } ui
..
我以前根据参数是否为 constexpr 函数重载。 > 。我正在尝试解决该问题令人失望的答案,以使它的断言功能更智能。这大概是我要尝试做的事情: 内联无效smart_assert(布尔条件){ if(is_constexpr( condition)) static_assert(condition,“ Error !!!”); 否则 断言(条件); } 基本上,这
..
我在 MyLiteral.h 中定义了以下用户定义的文字: namespace my_literals { constexpr uint64_t运算符“” _nanoseconds(无符号long long int值){ 返回值* 1000; } } 现在我可以在另一个标头中使用运算符 SomeComponent.h : 使用命名空间my_literals;
..
我对C ++ 11中的静态constexpr 成员变量有些困惑。 first.hpp template struct cond_I {static constexpr T value = 0; }; //专业化 template struct cond_I > {
..
使用C ++ 11,Ubuntu 14.04,GCC默认工具链。 此代码失败: constexpr std :: string constString =“ constString”; 错误:类型'const string {aka const std :: basic_string }' constexpr变量'constString'不是文字的...因
..
我已经定义了constexpr函数,如下所示: constexpr int foo(int i) { 返回i * 2; } 这就是主要功能: int main() { int i = 2; cout
..
在 中,自C ++ 11起,存在以下两个重载: std :: intmax_t abs( std :: intmax_t n); std :: intmax_t imaxabs(std :: intmax_t n); 为什么这两个函数不是 constexpr ? 解决方案 我无法给出为什么 abs 的充分理由不能是 constexpr
..
我想在lambda中使用constexpr值。阅读 使用lambda捕获的constexpr值作为数组维度的答案 ,我假定以下方法应该起作用: #include int main() { constexpr int i = 0; auto f = [] { std :: array一种; }; 返回0; } 但是,Cla
..
以下C ++ 11程序格式错误吗? const int x [] = {1,2, 3}; static_assert(x [0] == 1,“是”); int main(){} gcc和clang似乎这么认为,但为什么 x [0] == 1 不是常数表达式? x [0] == 1 下标运算符 *(x + 0)== 1 数组到指针的转换(i
..
任何仅包含return语句的函数都可以声明为 constexpr ,因此,如果所有 参数都可以在编译时进行求值是 constexpr ,并且仅在其主体中调用 constexpr 函数。 是否有任何理由不声明任何这样的函数 constexpr ? 示例: constexpr int sum(int x,int y){return x + y; } constexpr i = 1
..
使用C ++ 17,我们可以获得内联变量。 它们的用途之一是在类中定义常量字段。 那么这两个常量定义之间有什么区别: class MyClass { 静态constexpr int myFirstVar = 10; static const inline int mySecondVar = 100; }; 当然, constexpr 使 myFirstV
..
因此,我知道在C ++中,静态成员可以在类内部进行初始化,前提是它们是const文字类型,如下所示 class test { public: static constexpr int stc = 1; 私人: int a = 0; int b = 0; int c = 0; }; 和静态constexpr变量 stc 可以是在编译器可以直接替换成员值的地
..
字面值constexpr构造函数和函数(不同vc,g ++)的类编译错误显示,在C ++ 14中, constexpr int A :: a()和 constexpr A :: a()const 可以使用。即成员函数上的 constexpr 并不表示该函数不会更改其作用的对象。 给出的示例是: 结构A { constexpr A(){} constint int a() {返回1
..
我写了这样的代码 #include 使用命名空间std; constexpr int getsum(int to){ int s = 0; for(int i = 0; i s + = i; } return s; } int main(){ constexpr int s = getsum(10); cout
..
许多开发人员和库作者已经在编译时字符串方面苦苦挣扎了好几年了,因为标准(库)字符串 std :: string 要求动态内存分配,而不是constexpr。 因此,我们有很多问题和博客文章,介绍如何正确获取编译时字符串: 方便地在C ++中声明编译时字符串 在编译时将模板中的编译时字符串连接起来? C ++编译时字符串操作 (异地)使用constexpr的编译时字符串 我们
..
这是有效的代码: struct S { constexpr S(int x,int y):xVal (x),yVal(y){} constexpr S(int x):xVal(x){} constexpr S(){} const int xVal {0 }; const int yVal {0}; }; 但是在这里我真的想声明 xVal 和 y
..
为什么此 constexpr static 成员函数由 //标识! Nah 评论,被调用时不被视为 constexpr ? struct Item_id { 枚举Enum { 的大小,位置,属性,window_rect,max_window_size,_ }; 静态constexpr int n_items_ = _; // OK constexpr auto memb
..
有一个众所周知的技巧,通过执行类似以下操作,会导致在 constexpr 函数的评估中导致编译时错误: constexpr int f(int x){ return(x!= 0)? x:抛出std :: logic_error(“哦,不!”); } 如果该函数用于 constexpr 上下文,如果 x == 0 ,您将收到编译时错误。但是,如果 f 的参数不是 con
..