c++ - 在依赖于类型的上下文中使用 constexpr

标签 c++ templates metaprogramming

简单的问题,这合法吗?

template<class T>
struct foo {

using type = std::conditional<IF_CONDITION<T>::value, constexpr int, int>::type;

};

编译器错误:“错误:“constexpr”在这里无效” 一直无法找到关于此的任何文档。因为它是一个编译时条件,所以看起来这至少在理论上应该能够做到。

最佳答案

来自标准:

The constexpr specifier shall be applied only to the definition of a variable or variable template or the declaration of a function or function template. A function or static data member declared with the constexpr specifier is implicitly an inline function or variable (10.1.6). If any declaration of a function or function template has a constexpr specifier, then all its declarations shall contain the constexpr specifier. [ Note: An explicit specialization can differ from the template declaration with respect to the constexpr specifier. — end note ] [ Note: Function parameters cannot be declared constexpr. — end note ]

所以 constexpr 不应该在模板参数中。

在您的情况下,您可以创建类型特化别名:

template<typename T>
using MyVec0 = Vector<T,0>;

关于c++ - 在依赖于类型的上下文中使用 constexpr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48195471/

相关文章:

c++ - 使用openGL的glBindFramebuffer好像没有效果

c++ - 如何为编译时已知的参数的多个值编译函数

C++ 模板函数特化

C++:你在使用 Loki 还是 Boost 作为仿函数?

c++ - 对多个索引数据数组的面向数据的访问

c++ - 执行 C++ 程序并使用 Perl 复制 cmd 输出

C++ 元编程 : Store integer by type

c++ - 尽管有 static_assert,但默认模板匹配

ruby - 对于未定义的实例变量,Ruby 是否有 method_missing 等价物?

c++ - 拆分 C++ TypeList 的最优雅方式