-
25 votesanswersviews
constexpr if和static_assert
P0292R1 constexpr if已经included,正在进行C17 . 它似乎很有用(并且可以取代SFINAE的使用),但是有关 static_assert 格式错误的评论,假分支中不需要诊断让我感到害怕: Disarming static_assert declarations in the non-taken branch of a constexpr if is not propo... -
2 votesanswersviews
为什么在Visual C 2015 update 3中没有使用constexpr自动返回类型推导?
此代码在GCC 4.9和Clang 3.5(带有标志-std = c 14)中编译并运行良好,但在VC 2015中给出了编译错误 . 请注意 operator* 中的自动返回类型: #include <cstdio> struct A { int i; constexpr A() : i(0) {}; constexpr A(int i) : i(i) {};... -
427 votesanswersviews
`constexpr`和`const`之间的区别
constexpr 和 const 之间有什么区别? 我什么时候才能只使用其中一个? 我何时可以同时使用两者?如何选择? -
5 votesanswersviews
Constexpr decltype
我最近在这里问了一个问题(Detecting instance method constexpr with SFINAE),我试图在编译时进行一些constexpr检测 . 最后,我发现可以利用 noexcept 来做到这一点:任何常量表达式也是 noexcept . 所以我把以下机器放在一起: template <class T> constexpr int maybe_noexc... -
20 votesanswersviews
实现is_constexpr_copiable
我试图实现一个类似于 std::is_constructible 的值模板,但只有在constexpr环境中该类型可复制时才会生效(即其复制构造函数是constexpr限定的) . 我到达了以下代码: #include <type_traits> struct Foo { constexpr Foo() = default; constexpr Foo(const F... -
19 votesanswersviews
为什么在类中初始化的非整数静态数据成员必须是constexpr?
在类定义中初始化的静态整数数据成员可以声明为 const 或 constexpr ,但在类定义中初始化的非整数静态数据成员必须是 constexpr : class MyClass { static const int w = 5; // okay static constexpr int x = 5; // okay static ... -
7 votesanswersviews
静态constexpr成员的统一初始化
根据:constexpr static data member giving undefined reference error静态constexpr类成员必须满足两个要求: template <typename Tp> struct wrapper { static constexpr Tp value{}; // 1 }; template<typename Tp>... -
8 votesanswersviews
SFINAE constexpr与std :: get
这是Detecting constexpr with SFINAE的后续问题 . 我想检测元组的元素(或任何可与 std::get 一起使用的元素)是否为constexpr . 所以我编写了以下助手,类似于Xeo给出的: template<size_t> struct sfinae_true : std::true_type{}; template<size_t N, clas... -
9 votesanswersviews
static constexpr指向函数的指针,编译器之间的区别
在回答this question时,我使用gcc(code compiled)和clang(code rejected)尝试了以下代码: typedef long (*func)(int); long function(int) { return 42; } struct Test { static constexpr func f = &function; }; templ... -
13 votesanswersviews
允许在constexpr函数中进行函数指针比较吗?
假设我有一个constexpr函数指针数组,我想编写一个constexpr函数来查找指定函数的数组索引 . 我可能有这样的代码: void test1(){}void test2(){}void test3(){}void test4(){} typedef void(*func)(void); constexpr func funcs[] = { &test1, &test2,...