[英]static_assert and Intel C++ compiler
閱讀cppreference.com :
靜態斷言聲明可以出現在塊作用域(作為塊聲明)和類體內 (作為成員聲明)
好的,現在我有以下代碼:
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:表達式未求出常量”,后者顯示“函數調用必須在常量表達式中具有常量值”錯誤,並指向) static_assert
maxAlignment
)。
但是MSVC 2015 Update 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
(因此,MSVC可以在模板類主體中處理static_assert
)
但是以下代碼已被兩個編譯器成功編譯( 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 ++編譯器的錯誤?
如果我記得沒錯,那么在完全定義constexpr函數之前,不能使用constexpr函數,直到定義了類,才可以定義類成員constexpr函數,這意味着您不能在類作用域static_assert
內使用constexpr成員函數。定義了這個功能。
但是您可以使此函數獨立(無論如何它都是靜態的),它將完成工作。 它應該在任何地方編譯。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.