为什么 VC++ 2010 编译器在编译简单代码时崩溃?

发布于 2024-10-05 05:24:31 字数 1913 浏览 7 评论 0原文

我遇到了一个非常奇怪的症状。谁能告诉我根本原因是什么?

我的VC++编译器版本是最新的:“Microsoft Visual C++ 2010:01019-532-2002102-70860”

重现步骤:

  1. 创建一个空的win32控制台项目
  2. 添加一个名为 main.cpp 的新 cpp 文件
  3. 将以下代码粘贴到 main.cpp 中
  4. 编译
  5. 编译器崩溃并报告以下消息:

\bug\main.cpp(54893757):致命错误 C1001:发生内部错误 在编译器中。 (编译器文件 'msc1.cpp',第 1420 行)

要解决此问题,请尝试 简化或改变程序 靠近上面列出的位置。 请选择技术支持 Visual C++ 帮助菜单上的命令, 或打开技术支持帮助 文件以获取更多信息。

注入文本中发生此错误:

d:\bug\main.cpp(63) :参见参考 函数模板实例化 'XDummy Test(T)' 正在使用 [ 进行编译 T=int]

构建失败。

下面是main.cpp的源代码:

#include <vector> 

template<class It_> 
struct trait_dummy 
{ 
    static const int value = std::tr1::is_convertible<typename iterator_traits<It_>::iterator_category, int>::value;     
}; 

template<class It_> 
class X 
{ 
public: 
    template<class T_> 
    X(T_& rColl) 
    {} 
}; 

template<class T_> 
X<typename T_::iterator> f(T_ rColl, std::false_type) 
{ 
    return X<typename T_::iterator>(rColl); 
} 

template<class T_> 
auto f(T_& rColl) -> decltype(f(rColl, std::false_type())) 
{ 
    return f(rColl, std::false_type()); 
} 

template<class It_> 
X<It_> f(It_ first, size_t nSize, typename std::tr1::enable_if<trait_dummy<It_>::value>::type* dummy = 0) 
{ 
    return X<It_>(first, first + nSize); 
} 

class XTest 
{ 
public: 
    void foo() 
    { 
        auto v = f(m_Suite); 
    }    

    std::vector<int> m_Suite; 
}; 

const int g_dummy = 0; 
class XDummy 
{ 
public: 
    XDummy(int, int, int, int dummy = g_dummy) 
    {} 
}; 

template<class T> 
XDummy Test(T) 
{    
    return XDummy(0, 0, 0); 
} 

int main() 
{ 
    Test(0); 
    //XTest().foo(); 

    return 0; 
}

I encountered a very strange symptom. Who can tell me what the root cause is?

My VC++ compiler version is latest: "Microsoft Visual C++ 2010 : 01019-532-2002102-70860"

Steps to reproduce:

  1. Create an empty win32 console project
  2. Add a new cpp file named main.cpp
  3. Paste the following code into main.cpp
  4. Compile
  5. The compiler crashes and reports the following message:

\bug\main.cpp(54893757): fatal error
C1001: An internal error has occurred
in the compiler. (compiler file
'msc1.cpp', line 1420)

To work around this problem, try
simplifying or changing the program
near the locations listed above.
Please choose the Technical Support
command on the Visual C++ Help menu,
or open the Technical Support help
file for more information.

This error occurred in injected text:

d:\bug\main.cpp(63) : see reference to
function template instantiation
'XDummy Test(T)' being compiled with [
T=int ]

Build FAILED.

Below is the source code of main.cpp:

#include <vector> 

template<class It_> 
struct trait_dummy 
{ 
    static const int value = std::tr1::is_convertible<typename iterator_traits<It_>::iterator_category, int>::value;     
}; 

template<class It_> 
class X 
{ 
public: 
    template<class T_> 
    X(T_& rColl) 
    {} 
}; 

template<class T_> 
X<typename T_::iterator> f(T_ rColl, std::false_type) 
{ 
    return X<typename T_::iterator>(rColl); 
} 

template<class T_> 
auto f(T_& rColl) -> decltype(f(rColl, std::false_type())) 
{ 
    return f(rColl, std::false_type()); 
} 

template<class It_> 
X<It_> f(It_ first, size_t nSize, typename std::tr1::enable_if<trait_dummy<It_>::value>::type* dummy = 0) 
{ 
    return X<It_>(first, first + nSize); 
} 

class XTest 
{ 
public: 
    void foo() 
    { 
        auto v = f(m_Suite); 
    }    

    std::vector<int> m_Suite; 
}; 

const int g_dummy = 0; 
class XDummy 
{ 
public: 
    XDummy(int, int, int, int dummy = g_dummy) 
    {} 
}; 

template<class T> 
XDummy Test(T) 
{    
    return XDummy(0, 0, 0); 
} 

int main() 
{ 
    Test(0); 
    //XTest().foo(); 

    return 0; 
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

把昨日还给我 2024-10-12 05:24:31

您自己尝试过任何类型的故障排除吗?

我可以按照您的描述使用上述源代码重现崩溃。当然,我收到一些警告:

  • “IntelliSense:没有重载函数“f”的实例与参数列表匹配”
  • “IntelliSense:函数调用中的参数太少”

都引用了这一行:

auto v = f(m_Suite); 

再进行几秒钟的故障排除发现通过注释掉整个 XTest 类,代码可以毫无问题地编译和执行(最重要的是,不会导致编译器崩溃)。这告诉我(并且应该告诉您)问题显然出在 XTest 类中的某个地方。
您不禁想知道这是否与正在发生的编译器错误有关。生成的。

好吧,如果我们只是注释掉产生编译器错误的那一行怎么样?你知道什么!代码编译并执行得很好!

因此,在大约一分钟的时间里,我们将罪魁祸首缩小到一行代码。不过,我不会真正花时间去准确理解所有代码的作用,因为我认为您现在可以从这里开始,因为您确切地知道应该在哪里集中精力。首先修复这些 IntelliSense 错误,然后看看您的代码是否可以编译而不会导致编译器崩溃。

Have you tried any type of troubleshooting yourself?

I can reproduce the crash using the above source code as you describe. Of course, I get a couple of warnings:

  • "IntelliSense: no instance of overloaded function "f" matches the argument list"
  • "IntelliSense: too few arguments in function call"

both referring to this line:

auto v = f(m_Suite); 

A few more seconds of troubleshooting discovers that by commenting out the entire XTest class, the code compiles and executes without a problem (and most importantly, without crashing the compiler). That tells me (and should tell you) that the problem clearly lies somewhere within the XTest class.
You can't help but wonder if that has something to do with the compiler errors that are being generated.

Well, how about if we just comment out that single line that's producing the compiler errors? What do you know! The code compiles and executes just fine!

So in under about a minute, we've narrowed down the culprit to a single line of code. I'm not going to actually take the time to understand exactly what all of your code does, though, since I think you can take it from here now that you know exactly where to concentrate your efforts. Start by fixing those IntelliSense errors and see if your code compiles without crashing the compiler.

旧人 2024-10-12 05:24:31

编译器作者不会非常重视修复编译器中的错误,因为这些错误不会影响编译器从有效输入生成有效输出的能力。在这些错误中,最低优先级是那些不会默默产生无效输出的错误。

所以你的问题的答案很可能是因为这个错误以前没有被报告过或者被分配了一个非常非常低的优先级。

Compiler authors don't put a very high priority into fixing bugs in their compiler that don't affect the compiler's ability to generate valid output from valid input. And among those bugs, the very lowest priority goes to those bugs that don't silently produce invalid output.

So the answer to your question is that most likely it's either because this bug hasn't been previously reported or has been assigned a very, very low priority.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文