Re: String literals and templates
"Aaron Graham" <atgraham@gmail.com> schrieb im Newsbeitrag
news:1155697904.152036.274270@h48g2000cwc.googlegroups.com...
What about overloading foo() as follows?
template <typename T> void foo(const T& t);
template <typename T> void foo(const T* t);
I don't know what the standard has to say about this, but on Visual C++
8.0
this seems to work as you might expect...
This is as close as I've come to a solution. I can overload it:
template <typename T> void foo(const T& t);
void foo(const char* t);
However, for the more general case, this isn't as pretty as you may
think. Imagine the following function (which is closer to my actual
problem):
template <typename T, typename U, typename V>
void foo(const T& t, const U& u, const V& v);
Now, to provide an equivalent solution for this one, I would need to
define 7 more overloaded functions.
What about the following solution, based on the posting of Gennaro Prota:
template <typename T, typename U, typename V>
void foo(const T& t, const U& u, const V& v);
template <class T, std::size_t N>
const T* bar( const T (&x)[N] )
{
return static_cast<const T*>( x );
}
int main()
{
foo( bar( "a" ), int(), float() );
foo( double(), bar( "a" ), char() );
return 0;
}
The idea is to explicitly convert an array to a pointer.
--
Matthias Hofmann
Anvil-Soft, CEO
http://www.anvil-soft.com - The Creators of Toilet Tycoon
http://www.anvil-soft.de - Die Macher des Klomanagers
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]