【问题标题】:C1001: An internal error has occurred in the compilerC1001:编译器发生内部错误
【发布时间】:2011-08-19 22:03:13
【问题描述】:

这应该是不言自明的。我正在尝试实现分发排序,而 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
}

【问题讨论】:

  • 尝试在connect.microsoft.com提交错误
  • 嗯,不确定你的错误,但pretty printer 对成员类型和成员函数都有 SFINAE 特征,你可以看看。
  • @Kerrek 漂亮的打印机代码声称“SFINAE 类型特征来检测“T::const_iterator T::begin/end() const”是否存在”,但它实际上会检查任何名为“begin”的成员”和“结束”。不是专门的函数...似乎编写lib的人部分复制了一些stackoverflow答案,但不了解它们是如何工作的。
  • 呵呵,谢谢 :-) 目的是让开始/结束检查来实际检查函数是否也具有正确的返回类型,但可能在途中丢失了。让我重新检查一下..
  • @Johannes:我创建了一个separate typetrait 来验证开始/结束成员的类型。它现在适用于简单的示例和反例,尽管它还不适用于完整的 ppdemo.cpp。一旦它经过全面测试,我将更新漂亮的打印机。感谢您指出这个遗漏!

标签: c++ templates sfinae typetraits


【解决方案1】:

这可能不是你想要的:

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

使用typename 告诉编译器C::func 将是一个类型。实际上它是一个函数,在参数声明中放一个函数名没有任何意义。

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

【讨论】:

  • 这可以防止编译器崩溃。嘘微软!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-09-16
  • 1970-01-01
  • 1970-01-01
  • 2011-08-26
  • 2019-05-23
  • 1970-01-01
相关资源
最近更新 更多