静态断言声明可以出现在块作用域(作为块声明)和类体内部(作为成员声明)
好的,现在我有以下代码:
struct foo_t
{
static constexpr std::size_t maxAlignment()
{
// This is just a sample; I removed real code from this method.
return std::max(alignof(__m128), __alignof(__m256));
}
static_assert(0 == ((maxAlignment() -1) & maxAlignment()), "some message");
};
既不是MSVC 2015也不是Intel C++ 16.0.2编译这段代码(前者显示“错误C2131:表达式没有评估为常量”,后者显示“静态断言中必须具有恒定值的函数调用”错误并指向对maxAlignment
的调用)。
但是MSVC 2015更新1可以编译以下代码,而Intel C++ 16.0.2不能:
template <typename T, std::size_t Alignment>
struct foo_t
{
static constexpr std::size_t maxAlignment()
{
return std::max(std::alignment_of<T>::value, Alignment);
}
static_assert(0 == ((maxAlignment() -1) & maxAlignment()), "some message");
};
foo_t<__m128, 16> foo {};
// foo_t<__m128, 33> boo {}; // here `static_assert` will stop compilation
所以,当static_assert
在模板类体内时,MSVC可以处理它。
但以下代码被两个编译器成功编译(static_assert
位于类体外部;它出现在块作用域中):
struct foo_t
{
static constexpr std::size_t maxAlignment()
{
return std::max(alignof(__m128), __alignof(__m256));
}
};
static_assert(0 == ((foo_t::maxAlignment() -1) & foo_t::maxAlignment()), "some message");
我的问题是:我有没有漏掉什么,还是这是英特尔 C++ 编译器的错误?
static_assert
在类体内部时)。 - Ruslan Garipovconstexpr
方面存在很多问题;我们在GCC和MSVC中遇到了与代码生成相关的错误。 - Crashworks