【发布时间】:2017-04-19 12:44:24
【问题描述】:
我想使用 constexpr bool(以下示例中的useF)来启用以下代码中的功能。在这里,打电话给A::f()。此外,在我关闭该功能的情况下,我希望将别名模板 (a) 变为 void。
我尝试使用 constexpr if 语句,但主体仍在被实例化,这会导致编译错误。如果我使用包装器模板 (X),则正文将按我的预期被丢弃,但这对我来说似乎很难看。有没有其他方法可以做到这一点?
constexpr bool useF = false;
struct A {
static void f() {}
};
using a = std::conditional<useF, A, void>::type;
template<typename L>
struct X {
static void h() {
if constexpr(std::is_same<L, A>::value) {
L::f(); // not instantiated, no error
}
}
};
int main() {
if constexpr(useF) {
a::f(); // error!?
}
X<a>::h();
}
我正在使用带有 -std=c++17 的 g++-7.0.1
【问题讨论】:
-
抱歉,标签选择错误!如果我选择标签 c++17,则插入标签 c++1z ...奇怪。
-
因为它是synonym。
标签: c++ c++17 if-constexpr