Template specialization not working...

R

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 T> T 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?
 
I

Ian Collins

Rob said:
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 T> T 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.
 
K

kwikius

----- Original Message -----
From: "Rob" <[email protected]>
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 T> T 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 );

}
 
J

James Kanze

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 T> T 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.

//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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,490
Latest member
Finplus

Latest Threads

Top