我昨天读了几个关于在an 子句中使用的答案。我知道根据标准,它被认为是格式错误的(即使某些编译器,包括 MSVC2017,会接受它)。Qt 也会将此标记为错误。static_assert(false, "Some message")elseif constexpr
我的问题是,下面的代码是否符合标准?(我倾向于这么认为,但我想确认一下。)
template <typename TypeOfValue>
static void PushValue(duk_context* ctx, TypeOfValue value) {
// Push value onto duktape stack
if constexpr (std::is_same<TypeOfValue, int>::value) {
// Push int
duk_push_int(ctx, value);
} else if constexpr (std::is_same<TypeOfValue, uint32_t>::value) {
// Push uint
duk_push_uint(ctx, value);
} else {
// Unsupported type
static_assert(bool_value<false, TypeOfValue>(), "Unsupported type");
}
}
template <bool value, typename T>
static constexpr bool bool_value() {return value;}
Run Code Online (Sandbox Code Playgroud)
编辑: …
template <typename>
struct Foo {
template <typename T>
struct Bar {
Bar(T val):
val_(val)
{
}
T val_;
};
};
auto func() {
return Foo<int>::Bar(3);
}
Run Code Online (Sandbox Code Playgroud)
在clang中似乎需要一个额外的推导指南,以便编译:
template <typename>
struct Foo {
template <typename T>
struct Bar {
Bar(T val):
val_(val)
{
}
T val_;
};
template <typename T>
Bar(T) -> Bar<T>; // deduction guide
};
auto func() {
return Foo<int>::Bar(3);
}
Run Code Online (Sandbox Code Playgroud)
这个额外要求背后有什么理由吗?或者只是对标准中不清楚的东西的解释有所不同?