ネストされたクラスを使用した奇妙なenable_ifの動作(MSVCコンパイラのバグまたは機能?)
-
13-09-2020 - |
質問
私のコードをかなりデバッグした後、私はenable_ifを使用している予期しないテンプレートの専門化結果に対する私の問題の理由を追跡しました:
次のコードは、Visual Studio 2010(および2008)のDotest()でアサーションを失敗しますが、G ++ 3.4.5では含まれません。 ただし、 someclass からテンプレートを削除するか、 someclass の範囲外の範囲外の範囲外の範囲内では、MSVCでも機能します。
この行動を説明する(少なくとも部分的に)、またはこれはMSVCコンパイラのバグですか?
(この例を使用して、ブーストとC ++ 0x STLバージョンと同じ)
#include <cassert>
#include <boost\utility\enable_if.hpp>
template <class X>
class SomeClass {
public:
template <class T>
struct my_condition {
static const bool value = true;
};
template <class T, class Enable = void>
struct enable_if_tester {
bool operator()() { return false; }
};
template <class T>
struct enable_if_tester<T, typename boost::enable_if< my_condition<T> >::type> {
bool operator()() { return true; }
};
template <class T>
void DoTest() {
enable_if_tester<T> test;
assert( test() );
}
};
int main() {
SomeClass<float>().DoTest<int>();
return 0;
}
.
範囲外の状態を動かすことで修正しようとしているときは、std :: enable_ifを使用するときにこれが十分ではないことに気付きましたが、少なくともBoost :: enable_if:
#include <cassert>
//#include <boost\utility\enable_if.hpp>
#include <type_traits>
template <class T, class X>
struct my_condition {
static const bool value = true;
};
template <class X>
class SomeClass {
public:
template <class T, class Enable = void>
struct enable_if_tester {
bool operator()() { return false; }
};
template <class T>
//struct enable_if_tester<T, typename boost::enable_if< my_condition<T, X> >::type> {
struct enable_if_tester<T, typename std::enable_if< my_condition<T, X>::value >::type> {
bool operator()() { return true; }
};
template <class T>
void DoTest() {
enable_if_tester<T> test;
assert( test() );
}
};
int main() {
SomeClass<float>().DoTest<int>();
return 0;
}
.
私は誰かがこれについての説明を持っていることを願っています。
解決
すべてのコードで問題ありません、それはVCがバグのあるということだけです。テンプレートメンバークラスの部分テンプレートの専門化に関する問題があることが知られています。
所属していません StackOverflow