constexpr相关内容
我试图初始化一个 constexpr 引用,但没有成功.我试过了 #include constexpr int&f(int& x)//可以定义返回 constexpr 引用的函数{返回 x;}int main(){constexpr int x{20};constexpr const int&z = x;//此处出错} 但我收到编译时错误 错误:constexpr 变量 'z' 必须由常
..
为什么不编译: string 作为返回类型会不会有问题? constexpr std::string fnc(){返回 std::string("Yaba");} 解决方案 std::string 的构造函数接受一个指向 char 的指针不是 constexpr.在 constexpr 函数中,您只能使用 constexpr 的函数.
..
我想尝试将项目从 gcc 迁移到 clang++.我承认我的无知,我不知道为什么下面的代码 template constexpr T pi{std::acos(T(-1.0))}; 使用 g++ 静默编译,但 clang++ 产生错误 trig.hpp:3:13: 错误:constexpr 变量 'pi'必须由常量表达式初始化constexpr T pi{std::acos(T(-1.0))
..
如果我尝试编译以下 C++0x 代码,则会出现错误: template结构 foo { };结构栏{静态 constexpr int number() { return 256;}void 函数(foo &);}; 使用 gcc 4.6.1,错误信息是: test.cc:6:27: 错误:在定义之前使用了‘static constexpr int bar::number()’test.cc:
..
我使用的是 g++4.8.0,它不包含早期的 constexpr 错误.因此下面的代码工作罚款: constexpr int size() { return 5;}int数组[size()];int main(){} 但是,如果我将 class 中的变量都作为 static 括起来,那么它会给出 编译器错误: struct X {constexpr static int size() { r
..
此 constexpr 代码未在 Visual Studio 2013 版本 12.0.21005.1 REL 中编译 是否有更新的 Visual Studio 编译器可以与 constexpr 配合使用? #include constexpr int factorial(int n){返回 n 编译输出: 1>------ 构建开始:项目:Project1,配置:调试 Win32
..
这可能是一个基本问题,但我现在看不到自己的回复. 考虑以下代码: template结构体{静态 constexpr int 值 = (b ? 42 : 0);};模板结构体{枚举 { 值 = (b ? 42 : 0) };};int main() {static_assert(T::value == 42, "!");static_assert(T::value == 0, "!");st
..
我想在编译时使用类型的名称.例如,假设我写了: constexpr size_t my_strlen(const char* s){常量字符* cp = s;while(*cp != '\0') { cp++;};返回cp-s;} 现在我想要: template constexpr auto type_name_length = my_strlen(typeid(T).name()); 但
..
struct 测试{static const int value = []() ->int { 返回 0;}();}; 使用 gcc-4.6 我得到类似错误:函数需要是 constexpr.我尝试了将 constexpr 放在不同地方的多种组合,但没有运气. constexpr 是否也支持 lambda 函数(无论是否指定了 return 类型)?什么是正确的语法? 任何可能的解决方
..
以下代码在 clang++ 3.7.0 下编译,但被 g++ 5.3.1 拒绝.两者都有 -std=c++14 选项.哪个编译器是正确的?任何人都知道标准中的哪里谈论这个?谢谢. #include 使用命名空间标准;constexpr int f(int n) {if (n 输出 [隐藏] g++ -std=c++14 c.cppc.cpp:在函数‘constexpr int f(int)
..
要求 我想要一个从 constexpr 函数计算的 constexpr 值(即编译时常量).而且我希望这两个范围都在类的命名空间内,即静态方法和类的静态成员. 第一次尝试 我首先用(对我来说)显而易见的方式写了这个: class C1 {constexpr 静态 int foo(int x) { 返回 x + 1;}constexpr 静态 int bar = foo(size
..
考虑以下代码: struct foo {static constexpr const void* ptr = reinterpret_cast(0x1);};自动主()->内部{返回0;} 以上示例在 g++ v4.9(Live Demo)中编译良好,但在clang v3.4 (Live Demo) 并生成以下错误: 错误:constexpr 变量“ptr"必须由常量表达式初始化
..
以下定义有区别吗? const double PI = 3.141592653589793;constexpr 双 PI = 3.141592653589793; 如果不是,那么在 C++11 中更喜欢哪种风格? 解决方案 我相信是有区别的.让我们重命名它们,以便我们可以更轻松地讨论它们: const double PI1 = 3.141592653589793;constexpr
..
我想使用 constexpr 填充枚举数组.数组的内容遵循一定的模式. 我有一个将 ASCII 字符集分为四类的枚举. 枚举类型{字母,数字,象征,其他,};constexpr 类型表 [128] =/* 等等等等 */; 我想要一个包含 128 个 Type 的数组.它们可以在一个结构中.数组的索引将对应于 ASCII 字符,值将是每个字符的 Type. 所以我可以查询这个数组来
..
假设我有一些 constexpr 函数 f: constexpr int f(int x) { ... } 而且我在编译时知道一些 const int N: 要么 #define N ...; 或 const int N = ...; 根据您的回答的需要. 我想要一个 int 数组 X: int X[N] = { f(0), f(1), f(2), ..., f(N-1)
..
我正在升级一些 C++ 代码以利用 C++11 中的新功能.我有一个 trait 类,其中有几个函数返回基本类型,这些函数在大多数情况下(但并非总是)返回一个常量表达式.我想根据函数是否为 constexpr 来做不同的事情.我想出了以下方法: template结构测试{模板静态 std::true_type do_call(int){ return std::true_type();}静态 s
..
P0292R1 constexpr if 已经
..
我试图声明一个 constexpr 指针初始化为某个常量整数值,但 clang 挫败了我的所有尝试: 尝试 1: constexpr int* x = reinterpret_cast(0xFF);test.cpp:1:20: 注意:常量表达式中不允许 reinterpret_cast 尝试 2: constexpr int* x = (int*)0xFF;test.cpp:1:20:
..
既然可以在运行时调用声明为 constexpr 的函数,那么编译器根据什么标准决定是在编译时还是在运行时计算它? templateconstexpr base_t POW(base_t base, expo_t expo){返回(世博会!= 0)?base * POW(base, expo -1) : 1;}int main(int argc, char** argv){int i = 0;st
..
我想在我的班级中有一个 static const char 数组.GCC 抱怨并告诉我应该使用 constexpr,尽管现在它告诉我这是一个未定义的引用.如果我将数组设为非成员,则它会编译.这是怎么回事? //.hpp结构 foo {空栏();静态 constexpr char baz[] = "quz";};//.cppvoid foo::bar() {std::string str(baz)
..