「 C++クラスにデフォルトのコンストラクター(コンパイラーが提供する型特性以外)があるかどうかをテストする方法はありますか? 」のコードを使用しました。
すべてのテストケースで機能するように少し変更しました。
template< class T >
class is_default_constructible {
typedef int yes;
typedef char no;
// the second version does not work
#if 1
template<int x, int y> class is_equal {};
template<int x> class is_equal<x,x> { typedef void type; };
template< class U >
static yes sfinae( typename is_equal< sizeof U(), sizeof U() >::type * );
#else
template<int x> class is_okay { typedef void type; };
template< class U >
static yes sfinae( typename is_okay< sizeof U() >::type * );
#endif
template< class U >
static no sfinae( ... );
public:
enum { value = sizeof( sfinae<T>(0) ) == sizeof(yes) };
};
2つのテンプレート引数バージョンでは正しく機能するのに、通常のバージョン(set #if 0
)では機能しないのはなぜですか?これはコンパイラのバグですか?VisualStudio2010を使用しています。
次のテストを使用しました。
BOOST_STATIC_ASSERT( is_default_constructible<int>::value );
BOOST_STATIC_ASSERT( is_default_constructible<bool>::value );
BOOST_STATIC_ASSERT( is_default_constructible<std::string>::value );
BOOST_STATIC_ASSERT( !is_default_constructible<int[100]>::value );
BOOST_STATIC_ASSERT( is_default_constructible<const std::string>::value );
struct NotDefaultConstructible {
const int x;
NotDefaultConstructible( int a ) : x(a) {}
};
BOOST_STATIC_ASSERT( !is_default_constructible<NotDefaultConstructible>::value );
struct DefaultConstructible {
const int x;
DefaultConstructible() : x(0) {}
};
BOOST_STATIC_ASSERT( is_default_constructible<DefaultConstructible>::value );
私はここで本当に途方に暮れています:
- 2つのテストが他のバージョンで失敗しています:
int[100]
とNotDefaultConstructible
。すべてのテストは、2つのテンプレート引数バージョンで成功します。 - VisualStudio2010はをサポートしていません
std::is_default_constructible
。ただし、私の質問は、2つの実装に違いがある理由と、一方が機能し、もう一方が機能しない理由についてです。