Templated Casting operators

N

Narinder

Hello,

Can't someone explain what should be the correct behaviour of the
following code:
__________________________________
#include<iostream>
using namespace std;

struct klass
{
template<class T>
operator T&()
{
throw("I am NON-const version");
}

template<class T>
operator const T&()
{
throw("I am CONST version");
}
};

int main()
{
try
{
klass k;
double x = k;
}

catch(const char *err)
{
cout << err << endl;
}
}
__________________________________

Compiled with MSVC 2010, language extensions disabled I get the
output: "I am CONST version"

Compiled with gcc version 4.5.2 (Ubuntu/Linaro 4.5.2-8ubuntu4) with
command line 'g++ -pedantic -ansi' I get the output :"I am NON-const
version"


Thanks in advance.
N
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top