以下代码在gcc和MSVC上编译通过,但是在clang上失败了(我测试了clang-3.5和当前的主干版本)。
template <typename T>
constexpr auto wrong = false;
template <typename T>
constexpr auto foo(const T t) -> int
{
static_assert(wrong<T>, "");
return {};
}
using F = decltype(foo(1));
int main() {}
clang在实例化函数体时遇到了static_assert
。gcc和MSVC只看函数声明,忽略函数体中的static_assert
。
如果移除constexpr,所有编译器都可以正常编译代码。
问题:
如果返回类型已声明,是否允许decltype查看函数体?
我正在寻找标准中相应部分的参考文献。
decltype
是否会触发模板实例化? - Ryan Hainingwrong
的特化是true
的情况。 - Rumburakconstexpr
,则没有编译器实例化该函数体。 - Rumburak