C1001:编译器中发生内部错误

Moo*_*uck 6 c++ templates sfinae type-traits

这应该是自我解释的.我正在尝试实现分发排序,并且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.
Run Code Online (Sandbox Code Playgroud)

(没有"上面列出的位置")最小的测试用例是:

#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
}
Run Code Online (Sandbox Code Playgroud)

sth*_*sth 5

这可能不是你想要的:

template <typename C> static yes& test( typename C::func ) ;
Run Code Online (Sandbox Code Playgroud)

typename你告诉编译器C::func将是一个类型.实际上它将是一个函数,并且在参数声明中放置函数名称没有任何意义.

您是否打算使用typeof而不是typename

  • 好吧,这会阻止编译器崩溃.嘘微软! (2认同)