The following code compiles successfully with gcc but fails to compile in Visual Studio 2013:
#include <memory>
#include <iostream>
using namespace std;
template <typename T>
class MyClass {
public:
MyClass(T* ptr) : m_ptr(ptr) {}
// All of the errors are caused by this conversion operator:
// =========================================================
template <typename U>
operator typename std::shared_ptr<U>() const & {
return m_ptr;
}
//==========================================================
private:
shared_ptr<T> m_ptr;
};
int main(int argc, char* argv[]) {
MyClass<int> c(new int(42));
shared_ptr<int> ptr = c;
cout << *ptr << endl;
}
I am fairly certain this is a bug in the VS2013 compiler because it does compile in newer versions of Visual Studio, but the problem is where I work, we are stuck using VS2013. Getting them to switch compilers is out of the question. (We have to use the same compiler that the vendor who supplies our software uses and they use VS2013.)
The errors are listed below:
Error 1 error C2143: syntax error : missing ';' before '&'
Error 2 error C2238: unexpected token(s) preceding ';'
Error 3 error C2988: unrecognizable template declaration/definition
Error 4 error C2059: syntax error : '<end Parse>'
Error 5 error C2334: unexpected token(s) preceding ':'; skipping apparent
Error 6 error C1004: unexpected end-of-file found
My question, is even with the limitations of VS2013, is there any workaround possible to get this code to compile and run successfully?