简单的问题,这合法吗?
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/