470,849 Members | 1,089 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,849 developers. It's quick & easy.

Template specialization not working...

Rob
I have a template function that works for 99% of the cases and want a
different one for the 1%. The problem is that the template case is too
"vague" and thus the compiler tries to use it for everything. AND the
template expects the same type put in as gotten out and the
specialization puts in a diff. type than it returns.

Code:
template <typename TT get(T a)
{
...
}

//The one case I want diff.
std::string get(SomeClass a)
{
...
}

int main()
{
std::string name( "Jeff" );

//This is correct
std::string val = get( name );

SomeClass a;

//Won't compile, says:
//cannot convert 'SomeClass' to 'std::string' in initialization
std::string val = get( a );
}

However, if I try template specialization, it just tells me that the
specializing template does not match any template declaration.

CODE:

//Attempting specialization, but says there's no matching declaring
template
template<std::string get(SomeClass a)
{
...
}

I think it's because the original template declaration takes and
returns the same type and the specialization takes and returns
different types. Any ideas?
Jun 27 '08 #1
3 1004
Rob wrote:
I have a template function that works for 99% of the cases and want a
different one for the 1%. The problem is that the template case is too
"vague" and thus the compiler tries to use it for everything. AND the
template expects the same type put in as gotten out and the
specialization puts in a diff. type than it returns.

Code:
template <typename TT get(T a)
{
...
}

//The one case I want diff.
std::string get(SomeClass a)
{
...
}

int main()
{
std::string name( "Jeff" );

//This is correct
std::string val = get( name );

SomeClass a;

//Won't compile, says:
//cannot convert 'SomeClass' to 'std::string' in initialization
The template should match here.
std::string val = get( a );
}

However, if I try template specialization, it just tells me that the
specializing template does not match any template declaration.

CODE:

//Attempting specialization, but says there's no matching declaring
template
template<std::string get(SomeClass a)
{
...
}
That isn't a specialisation, the return type differs from the parameter
type.

--
Ian Collins.
Jun 27 '08 #2

----- Original Message -----
From: "Rob" <so***************@yahoo.com>
Newsgroups: comp.lang.c++
Sent: Tuesday, April 29, 2008 1:22 AM
Subject: Template specialization not working...

>I have a template function that works for 99% of the cases and want a
different one for the 1%. The problem is that the template case is too
"vague" and thus the compiler tries to use it for everything. AND the
template expects the same type put in as gotten out and the
specialization puts in a diff. type than it returns.
<...>
I think it's because the original template declaration takes and
returns the same type and the specialization takes and returns
different types. Any ideas?
This works for me ...

#include <string>
template <typename TT get(T a)

{

return a;

}

struct SomeClass{};

//The one case I want diff.

std::string get(SomeClass a);

int main()

{

std::string name( "Jeff" );

//This is correct

std::string val = get( name );

SomeClass a;

std::string val1 = get( a );

}
Jun 27 '08 #3
On Apr 29, 2:22 am, Rob <someidunknown1...@yahoo.comwrote:
I have a template function that works for 99% of the cases and want a
different one for the 1%. The problem is that the template case is too
"vague" and thus the compiler tries to use it for everything. AND the
template expects the same type put in as gotten out and the
specialization puts in a diff. type than it returns.
Code:
template <typename TT get(T a)
{
...
}
//The one case I want diff.
std::string get(SomeClass a)
{
...
}
int main()
{
std::string name( "Jeff" );
//This is correct
std::string val = get( name );
SomeClass a;
//Won't compile, says:
//cannot convert 'SomeClass' to 'std::string' in initialization
std::string val = get( a );
This line won't compiler because val is already defined.
Changing it to:
val = get( a ) ;
and it compiles with the three compilers I have access to (g++,
Sun CC and VC++), calling the explicite overload in each case
(as it should).
}
However, if I try template specialization, it just tells me that the
specializing template does not match any template declaration.
CODE:
//Attempting specialization, but says there's no matching declaring
template
template<std::string get(SomeClass a)
{
...
}
Yes, because you're not specializing the same function. But it
doesn't matter---usually, you don't specialize template
functions, but simply provide an overload (as you originally
did).

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Agent Mulder | last post: by
6 posts views Thread by Gonçalo Rodrigues | last post: by
9 posts views Thread by stephen.diverdi | last post: by
6 posts views Thread by abir | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.