Topic: Q on template function specialization and overloading.
Author: "Bob Sanford" <Bob.Sanford@Compaq.com>
Date: 1997/10/11 Raw View
I would think you need to specify both template parameters to use it, like
so:
char c = 'a';
int x = implicit_cast< int, char >(c);
Maybe this won't help. I'm really just asking another question.
Bob.Sanford@Compaq.com
Chris Hines <ChrisHines@msn.com> wrote in article
<01bcd2cf$f110bb00$28012299@hermes>...
> In "The C++ Programming Language 3rd Ed." Bjarne Stroustrup gives an
> example of a template function that parameterizes the return type. This
is
> a powerful technique that I'm trying to use in real life, but I'm running
> into some problems with my compiler (VC++ 5.0 SP1). I'm curious what the
> standard says about this issue.
>
> Stroustrup's function is:
>
> template < class T, class U > inline T implicit_cast(const U& u) { return
> u; }
>
> It's is intended to be used as follows:
>
> char c = 'a';
> int x = implicit_cast< int >(c);
>
> Now my question is, how does this construct interact with function
> overloading. Let's say I call implicit_cast<>() again like so:
>
> long y = implicit_cast< long >(c);
>
> The compiler must now instantiate two versions of implicit_cast<>(const
> char&) with different return types. Normally functions cannot be
> overloaded based on return type, which is why this technique is so
> interesting. The explicit template parameter allows the user to specify
> the return type, thus removing the ambiguity that would normally cause
> problems.
>
> VC++ 5.0 SP1, handles this just fine ... until you start specializing the
> function. For example, if the code fragments above are put into a
complete
> program, they will work fine. Now, let's say I add some specializations
of
> implicit_cast<>(). (Note that I'm using implicit_cast<>() as an example
> here, I don't expect it to require specialization, but other functions
are
> likely to require specialization to provide the appropriate semantics.)
So
> let's say I specialize implicit_cast<>() as follows:
>
> template<> inline int implicit_cast< int >(const char& c) { return int(c)
> << 8; }
> template<> inline long implicit_cast< long >(const char& c) { return
> long(c) << 24; }
>
> VC++ seems to lose track of the templated origins of these these function
> definitions and treats them as normal functions, applying normal function
> overloading rules to them. The result is a compile time error.
>
> So, if there is anyone out there that can figure out what the standard
says
> should happen in this case, I'm curious to know.
>
> I have found a work around for VC++ 5.0 that involves a class template
with
> a template constructor and a conversion operator. The work around
> separates the source type from the return type without sacrificing the
> elegant syntax or the efficiency afforded by the template function. The
> only down side is that the class template form has an uglier
implementation
> than the function template form.
> --
>
> Chris Hines
> ---
> [ comp.std.c++ is moderated. To submit articles: try just posting with
]
> [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu
]
> [ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
]
> [ Policy:
http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
> [ Comments? mailto:std-c++-request@ncar.ucar.edu
]
>
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]