Re: Conversion Operator in Template Class
joecook@gmail.com wrote:
On Mar 18, 11:27 am, 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)
Victor said:
Because the Standard prohibits it. When deducing template arguments
from a function call, the conversions are not applied to the function
arguments.
Is there any way to get similar capability that you know of? What if
i wanted to have a class that behaved in everyway as say a
std::vector<T>, but I didn't want to inherit from std::vector<T> ? I
still want to pass this new class to functions that accept
std::vector<T>
Not in templates; you'd have to be explicit, and introduce a cast or a
function, like "as_vector":
template<class T> class Goo {
...
Foo<T>& as_Foo() { return m_foo; }
};
Goo<float> g;
bar(g.as_Foo());
.. I am not sure why such requirements are imposed, probably to allow
compilers actually be reasonable with respect to time they take to
compile templates. Imagine how many functions/class would have to be
instantiated if the compiler started applying all possible conversions,
especially in the cases with built-in types... But you probably can
find out more from C++ Templates by Vandevoorde and Josuttis and from
comp.std.c++ (where rationales behind Standard requirements are usually
discussed).
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask