【发布时间】:2015-10-06 00:45:42
【问题描述】:
以下代码触发static_assert,尽管我认为它不应该:
#include <type_traits>
template< typename T >
struct Tmp
{
~Tmp() noexcept( std::is_nothrow_destructible< T >::value ) {}
};
struct Foo;
struct Bar
{
// Comment this out for the problem to go away
Tmp< Foo > xx;
// ..or this
Bar() {}
};
struct Foo {};
// This triggers
static_assert( std::is_nothrow_destructible< Foo >::value, "That's odd" );
int main()
{
}
编译时:
g++-4.9 -std=c++11 nothrow_destructible_bug.cc
会发生以下情况:
nothrow_destructible_bug.cc:20:1: error: static assertion failed: That's odd
static_assert( std::is_nothrow_destructible< Foo >::value, "That's odd" );
^
为什么只使用Foo 实例化不相关类中的模板会使其失去noexcept 状态?我认为这是一个编译器错误,但我尝试了所有最新版本的 gcc 和 clang,它们似乎都给出了相同的错误。
【问题讨论】:
-
您是不是要检查
is_nothrow_destructible< Bar >而不是is_nothrow_destructible< Foo >?
标签: c++ c++11 exception exception-handling