constexpr相关内容
例如: constexpr int g(){return 30;}constexpr int f(){//我们可以省略const吗?const int x = g();const int y = 10;返回x + y;} 使用 const 在 constexpr 函数中声明局部变量吗? 具有 const 局部变量的 constexpr 函数是否与没有 const 的变量具有等效的功能
..
我有一个类模板,它根据模板参数作为其成员之一构建一个简单的数组.我需要能够在构造函数之一中将数组中的每个元素初始化为单个值.不幸的是,此构造函数必须为 constexpr . 相关部分归结为: 模板foo类{T data [N];constexpr foo(T val){//使用N个val副本初始化数据}}; 使用 std :: fill 或
..
当类具有constexpr成员函数并且正在constexpr上下文中的l值对象上对该成员函数进行求值时,clang和gcc会不同意结果是否为constexpr值.为什么?是否有既不需要默认可构造性又不需要复制可构造性的解决方法? 当对象通过值传递时,两个编译器均成功编译. Clang版本的trunk,8、7: static_assert表达式不是整数常量表达式 和 Gcc版
..
constexpr 说明符是否暗示函数的 noexcept 说明符?回答到简单检查显示出它的答案.并非如此. constexpr布尔f(int)noexcept{返回true;}constexpr布尔g(int){返回true;}static_assert(noexcept(f(1)));static_assert(noexcept(g(2)));//注释此行以检查运行时行为#include
..
我一直在学习C ++ constexpr 函数,并且实现了 constexpr 递归函数来查找第n个斐波那契数. #include#include#include#include#includeconstexpr long long fibonacci(int num){如果(num
..
以下代码在GCC(4.9.3)和VC ++(19.00.23506)中可以很好地编译,但是在Clang(3.7.0)中会出现这些错误. 错误:constexpr函数的返回类型'Foo'不是文字类型 注意:'Foo'不是文字的,因为它不是集合,并且没有复制或移动构造函数以外的constexpr构造函数 代码: #include#include
..
为什么constexpr变量的decltype失败? #include#includeconstexpr uint16_t foo(){return 0;}constexpr auto cv = foo();自动v = foo();static_assert(std :: is_same :: va
..
我想创建一个 std :: array 的对象,但是问题是我只能使用返回 constexpr 类型的函数,否则编译器会抱怨.这里的问题是,我需要根据另一个数组的大小来计算该数组的长度,这可能是这样的: 模板struct DataLength{模板size_t maxPossibleLength(开始,结束){size_t m_
..
我正在实现 constexpr int foo(); 函数.在 foo()的正文中,我想在编译时做一些不同的事情(并可能返回不同的东西),而在运行时做一些不同的事情. 对于C ++ 20,我可以使用 std :: is_constant_evaluated : constexpr int foo(){return std :: is_constant_evaluated()?123:45
..
我无法理解为什么gcc-8.2.0和clang-7.0.0都拒绝以下代码(实时代码这里): #includeint main(){constexpr std :: arrayv {1,2,3};constexpr auto b = v.begin();//错误:不是constexpr返回0;} 有错误 错误:'(std :: array :
..
我想用另一个 constexpr char [] 成员初始化 constexpr char [] 成员.是否可以在 C ++ 11 或更高版本中进行? #includestruct Base {静态constexpr char ValueOne [] =“一个";静态constexpr char ValueTwo [] ="Two";};模板st
..
我想创建一个 constexpr std :: array .例如,它应包含 constexpr std :: strings_view的,如下所示: “文本0",“文本1",“文本2",.....“文本" 我想出了以下初步解决方案: #include#include#i
..
这是我的问题 constexpr std :: array 的列表.但是,如果我想直接在初始化列表中使用我的类,则它将编译,但仅存储nullptrs.该程序的输出为: text123文字2╠╠╠╠╠╠╠╠╠╠╠╠╠╠
..
为什么不使用 auto 关键字定义变量时会带有用于初始化它的表达式的 constexpr 'ness? 作为示例,请考虑以下代码: #includeconstexpr std :: string_view f(){return"hello";}静态constexpr std :: string_view g(){constexpr auto x = f();/
..
(已实现为我的问题;如果您也对此感兴趣,我将不胜感激.) 如果std::array::size是constexpr ,那么为什么以下代码甚至无法编译? #include #include constexpr auto print_size = [](auto const& array){ constexpr auto size
..
我有这个 static_loop 构造,用于在循环展开时进行类型调度。 template void static_loop(F& f){ static_assert(n = 8) f(std :: integral_constant(
..
我正在阅读有关C ++中的 constexpr 和 static_assert 功能的信息,似乎让我感到困惑-我您已经读到 constexpr 函数不一定在编译期间就进行评估,它们有时可以在运行时进行评估。让我困扰的一件事是,在编译过程中总是检查 static_assert 。那么,如果我们将 constexpr 传递给 static_assert ,但是编译器选择评估该 constexpr,会发
..
下面的代码既不能在 gcc 中也不能在 clang 中进行编译。都抱怨说, reinterpret_cast 到 int * 不是 constexpr 。 如何解决该问题?请注意,我无法修改在第三方库( avr )中定义的宏 PORT 。 #include #定义端口((int *)(0x20)) constexpr int * p =端口; //不编译
..
编辑:重命名,因为我的最终解决方案不使用中毒方法。 我正在寻找一种防止在运行时调用constexpr方法的方法。我正在编写一个接受字符串文字的函数,所以我不能简单地使用NTTP作为要求 constexpr 参数的方式: template auto func(){...} 因为即使合法的constexpr使用也变得麻烦,要求值具有
..
我想让结构持有枚举 s进行迭代,以及 std :: string 保留其名称以创建菜单项。我使用的是这样的东西: struct S { 枚举E {一个,两个,全部}; std :: array名称{“一个”,“两个”}; }; int main() { S s; for(int i = s.ONE; i
..