6

これは一目瞭然です。ディストリビューション ソートを実装しようとしていますが、MSVC コンパイラがクラッシュします。メンバー関数を検出するために SFINAE を使用するのは特定のケースのようです。関数に indexert を渡さない場合や、has_get_index を置き換えない場合、これは発生しないようです。また、残りのインデクサー オーバーロードのいずれかを削除しても発生しません。sortable にgetIndex() constメンバーがある場合、問題は残ります。

1>test.cpp(34): fatal error C1001: An internal error has occurred in the compiler.
1>  (compiler file 'msc1.cpp', line 1420)
1>   To work around this problem, try simplifying or changing the program near the locations listed above.

(「上記の場所」はありません) 最小限のテスト ケースは次のとおりです。

#include <vector>
#include <iterator>
#include <type_traits>

#ifndef HAS_MEM_FUNC //SFINAE (or maybe it is?)
#define HAS_MEM_FUNC(name, func)                                        \
    template<typename T>                                                \
    struct name {                                                       \
        typedef char yes[1];                                            \
        typedef char no [2];                                            \
        template <typename C> static yes& test( typename C::func ) ;    \
        template <typename C> static no&  test(...);                    \
        static bool const value = sizeof(test<T>(0)) == sizeof(yes);    \
    }
#endif
HAS_MEM_FUNC(has_get_index,getIndex);

//default indexer undefined
template <class T>
double indexer(...);
//indexer for objects that have a "T::getIndex() const" member
template <class T>
double indexer(const typename std::enable_if<has_get_index<T>::value,T>::type& b) {
    return b.getIndex();
};

template<class indexert> 
void function(indexert indexeri)
{}

struct sortable {};

int main () {
    function(indexer<sortable>); //line 34
}
4

1 に答える 1

5

これはおそらくあなたが意図したものではありません:

template <typename C> static yes& test( typename C::func ) ;

型になることをコンパイラに typename伝えますC::func。実際には関数になり、パラメーター宣言に関数名を入れても意味がありません。

typeof代わりに使用するつもりでしたtypenameか?

于 2011-08-19T22:16:11.617 に答える