Re: Conversion Operator in Template Class
SG wrote:
On 18 Mrz., 16:27, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
joec...@gmail.com wrote:
Hmm... Why doesn't this work and use the conversion operator? (It
doesn't compile)
template<typename T>
class Foo
{
};
template<typename T>
void bar(Foo<T>& a){}
template<typename T>
class Goo
{
public:
operator Foo<U>(){return m_foo;}
Foo<U> m_foo;
};
int main()
{
Goo<float> g;
bar(g); // why doesn't this convert to Foo<U> ??
}
Because the Standard prohibits it. When deducing template arguments
from a function call, the conversions are not applied to the function
arguments.
In addition: The conversion would result in an rvalue which is not
allowed to bind to an lvalue reference to non-const.
In this particular test case, yes. But it's not going to be used even
if the conversion function is changed to
operator Foo<U>& () { return m_foo; }
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask