473,383 Members | 1,918 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,383 software developers and data experts.

template specialization overriding non-specialization?

I'm posting this question for a friend who lacks USENET access.
He and I were discussing this question and could not figure out the
solution.

Thank you for your help

Joseph

===

I have a template class with a bunch of methods that are used for most
of the possible template parameter types. However, for a couple of
types the implementation needs to be different. The solutions for
this given on
http://www.parashift.com/c++-faq-lit....html#faq-35.7 don't
seem to work. If I include the instantiations at the bottom of the
definitions (.cpp) file, the compiler seems not to notice the
specialization (in a separate .cpp file). If I include that other
..cpp file in the first one, the compiler complains about multiple
instantiations. Anybody have any suggestions or working examples of
this? I'm using gcc-4 on red hat linux.

TIA,

Apr 16 '06 #1
2 2177

Joseph Turian wrote:
I have a template class with a bunch of methods that are used for most
of the possible template parameter types. However, for a couple of
types the implementation needs to be different. The solutions for
this given on
http://www.parashift.com/c++-faq-lit....html#faq-35.7 don't
seem to work. If I include the instantiations at the bottom of the
definitions (.cpp) file, the compiler seems not to notice the
specialization (in a separate .cpp file). If I include that other
.cpp file in the first one, the compiler complains about multiple
instantiations. Anybody have any suggestions or working examples of
this? I'm using gcc-4 on red hat linux.


The reason for your problem is explained in FAQ 35.12:

http://www.parashift.com/c++-faq-lit...html#faq-35.12

The most straight-forward solution is to put both the template class
definition and all the specializations in one or more header files - do
not put any template definitions or specializations in a cpp file. If
you use the template or any specializations, make sure that cpp file
includes the appropriate header files prior to the use. Do not (in my
view, ever) include one cpp file in another - put it in the header
files.

If you want anything more specific, you're going to need to post code
that illustrates your problem. If you do so, keep it short and make
sure it is compilable (no ellipses or "[my code here"] - so that we can
copy it and compile it ourselves to see your problem.

Best regards,

Tom

Apr 16 '06 #2
Joseph Turian wrote:
Anybody have any suggestions or working examples of
this? I'm using gcc-4 on red hat linux.


As Thomas Tutone says it would help to post a minimal example of what
you are trying to do. It sounds like you want to specialise a member
function for one or more members without inlining the function. In the
header file declare but dont define the function:

// "test.hpp"
#ifndef TEST_HPP_INCLUDED
#define TEST_HPP_INCLUDED
template <typename T>
struct S{
void func();
};

#endif
//--------------------------

Then in a .cpp file do the definitions inc required specialisations
Followed by instantiation requests for any templates you need to use

// "test1.cpp"
#include "test.hpp"
#include <iostream>

template <typename T>
void S<T>::func(){
std::cout << "unspecialised\n";
}

template <>
void S<int>::func()
{
std::cout << "specialised\n";
}

// request instantiations AFTER showing the definitions
// and any specialisations
template
void S<int>::func();
template
void S<double>::func();

//--------------------------------

// link the object code from test1.cpp in your project

// check it works...

// "main.cpp"

#include "test.hpp"

int main()
{
S<double> s_double;
s_double.func();

S<int> s_int;
s_int.func();
}

//---------------------------

// should output:
unspecialised
specialised

HTH

regards
Andy Little

Apr 16 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Owen Jacobson | last post by:
Salve. I'm running into a compilation problem trying to write some code using libpqxx ( <http://pqxx.tk/> )'s pqxx::Connection and pqxx::Transactor classes. I seem to be running into a problem...
3
by: Cheng Mo | last post by:
When overriding operator new & delte of one class, the method is implicitly declared as static. However, overriding operator new & delete of template cannot be static.The compiler says cannot...
2
by: franklini | last post by:
hello people i. can anybody help me, i dont know what is wrong with this class. it has something to do with the me trying to override the input output stream. if i dont override it, it works fine....
17
by: Bob Weiner | last post by:
What is the purpose of hiding intead of overriding a method? I have googled the question but haven't found anything that makes any sense of it. In the code below, the only difference is that...
2
by: John Boers | last post by:
I am trying to create a template for my website. But I have a problem, the system says BC30469: Reference to a non-shared member requires an object reference and points to the statement...
3
by: David Scarlett | last post by:
Hi all, I've got a question regarding overriding const member functions with non-const functions. Let's say I've got a base class defined as follows: /*******************/ class Foo {...
45
by: charles.lobo | last post by:
Hi, I have recently begun using templates in C++ and have found it to be quite useful. However, hearing stories of code bloat and assorted problems I decided to write a couple of small programs...
272
by: Peter Olcott | last post by:
http://groups.google.com/group/comp.lang.c++/msg/a9092f0f6c9bf13a I think that the operator() member function does not work correctly, does anyone else know how to make a template for making two...
10
by: r035198x | last post by:
The Object class has five non final methods namely equals, hashCode, toString, clone, and finalize. These were designed to be overridden according to specific general contracts. Other classes that...
32
by: Stephen Horne | last post by:
I've been using Visual C++ 2003 for some time, and recently started working on making my code compile in GCC and MinGW. I hit on lots of unexpected problems which boil down to the same template...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.