【发布时间】:2018-01-10 20:22:41
【问题描述】:
简单的问题,这合法吗?
template<class T>
struct foo {
using type = std::conditional<IF_CONDITION<T>::value, constexpr int, int>::type;
};
编译器错误:“错误:“constexpr”在此处无效” 无法找到有关此的任何文档。因为它是一个编译时条件,所以这似乎至少在理论上应该能够做到。
【问题讨论】:
-
constexpr int不是类型 -
"const int" 将是一种类型。 "constexpr int" 不是类型。
-
我明白了,我认为它可以与
const媲美你知道他们是否有任何方法可以效仿吗? (类专业化除外) -
@JosephFranciscus 在什么样的代码中你需要有条件的 constexpr?也许我们可以帮助你。
-
我正在写一个像
Eigen这样的库,所以如果Rows == 0比我想要一个动态的Row 值(IE 常规int),一个对象会像Vector<class T, int Rows>,否则我想要它是constexpr(在编译时会知道)
标签: c++ templates metaprogramming