Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this code make the VC++ compiler crash?

I'm using the following compiler:

Microsoft Visual C++ 2010

The following code crashes the compiler when it's compiled:

template<class T_> 
void crasher(T_ a, decltype(*a)* dummy = 0){}

int main()
{
    crasher(0);
    return 0;
}

decltype(*a)* used to enforce T_ to be a pointer-like type - such as char*, int*, and shared_ptr<int>.

Why is it crashing? Is this a known bug?

like image 667
xmllmx Avatar asked Dec 03 '10 09:12

xmllmx


1 Answers

Assuming your goal is

decltype(*a)* used to enforce T_ to be a pointer-like type - such as char*, int*, and shared_ptr.

... what you need is simple template, not a code which happens to crash the compiler :)

Here is something that may work for you

#include <memory>
#include <iostream>

// uncomment this "catch all" function to make select(0) compile
// int select(...){ return 0;}
template<class T>  int select(T*){ return 1;}
template<class T>  int select(std::auto_ptr<T>){ return 1;}
// add boost::shared_ptr etc, as necessary

int main()
{
    std::cout << select(0) << std::endl;
    std::cout << select(std::auto_ptr<int>()) << std::endl;
    std::cout << select(&std::cout) << std::endl;
    return 0;
}
like image 154
bronekk Avatar answered Nov 11 '22 11:11

bronekk