Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does VC++ 2010 compiler crash when compiling simple code?

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; 
}
like image 978
xmllmx Avatar asked Nov 27 '10 05:11

xmllmx


1 Answers

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.

like image 126
Cody Gray Avatar answered Sep 21 '22 11:09

Cody Gray