473,400 Members | 2,145 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,400 software developers and data experts.

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 1101
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Agent Mulder | last post by:
Hi group, I have a problem with partial template specialization. In the code below I have a template struct Music with one method, play(), and three kinds of music, Jazz, Funk and Bach. When I...
6
by: Dave | last post by:
Hello all, Consider this function template definition: template<typename T> void foo(T) {} If foo is never called, this template will never be instantiated. Now consider this explicit...
6
by: NKOBAYE027 | last post by:
FIRST POST Hi All: I'm trying to write a simple specialization before moving on to something a bit more complex - always a good idea in my case, at least. :o) I'm trying to adapt the example...
3
by: Adam | last post by:
I'm trying to write a template specialization, and the char* specialization is giving me some trouble. Here's a simplified example: /**************************/ /* test.cpp */ #include...
6
by: Gonçalo Rodrigues | last post by:
Hi all, The following (test) program is not working as I expected. Note: The code is a bit long but is easy to understand: There is a class Object implementing ref counting. A Ref<T> template...
9
by: Marek Vondrak | last post by:
Hello. I have written the following program and am curious why it prints "1" "2". What are the exact effects of explicitly providing function template parameters at the call? Is the second...
9
by: stephen.diverdi | last post by:
Can anyone lend a hand on getting this particular template specialization working? I've been trying to compile with g++ 4.1 and VS 2005. ...
2
by: Barry | last post by:
The following code compiles with VC8 but fails to compiles with Comeau online, I locate the standard here: An explicit specialization of any of the following:
6
by: abir | last post by:
i have a template as shown template<typename Sclass Indexer{}; i want to have a specialization for std::vector both const & non const version. template<typename T,typename Aclass...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.