Re: Templated Casting operators
On Jul 8, 1:58 am, Alexander Bartolich <alexander.bartol...@gmx.at>
wrote:
Narinder wrote:
[...]
Can someone explain what should be the correct behaviour of the
following code:
Two functions in the same scope that differ only by return type?
My guess is that the behaviour is implementation defined.
struct klass
{
template<class T>
operator T&()
{
throw("I am NON-const version");
}
template<class T>
operator const T&()
{
throw("I am CONST version");
}
};
The second signature is missing a trailing "const":
operator const T&() const
--
host -t mx moderators.isc.org
I think if the behaviour is 'supposed' to be implementation-defined,
then that is a problem, isn't it ?
Consider the following code:
-------------------------------------------------------------
#include<iostream>
using namespace std;
double pi =3.142;
struct klass
{
template<class T>
operator T&()
{
cout << "I am NON-const version\n";
return pi;
}
template<class T>
operator const T&()const
{
cout << "I am CONST version\n";
return pi;
}
};
template<class T>
T& show(T &t)
{
cout << "NON-CONST show\n";
return t;
}
template<class T>
const T& show(const T &t)
{
cout << "CONST show\n";
return t;
}
int main()
{
klass k;
show(k);
show(klass());
double x1 = k;
double x2 = klass();
}
-----------------------------------------------------
Output from msvc :
NON-CONST show
CONST show
I am CONST version
I am CONST version
Output from gcc
NON-CONST show
CONST show
I am NON-CONST version
I am NON-CONST version
To me the msvc appears 'correct' or at least self-consistent. Or
perhaps I am missing something.
Any thoughts ?