473,832 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2195

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_INCLUD ED
#define TEST_HPP_INCLUD ED
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
3595
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 with templates that I do not understand. I have a class that provides a Transactor (a functor that operates on a database, in pqxx) to check for sessions. The interface is as follows: --SessionTransaction.hpp-- #ifndef SESSIONTRANSACTION
3
7807
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 declare the function a static linkage. Why? C++ is so complex and so many situation should be considered.
2
2477
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. i would forget overriding it but i have to do it because its a coursework. here is a simple version of the class #include <iostream> #include <string> #include <vector>
17
2924
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 when the Poodle is upcast to the Dog (in its wildest dreams) it then says "bow wow" where the bernard always says "woof" (see code). Basically, it appears that I'm hiding the poodle's speak method from everything except the poodle. Why would I...
2
1478
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 PageBase.Render( writer ). I create the template by overriding the System.Web.UI.Page.Render method. Then in every page I refer to the designed template. Please explain to me where I go wrong. Look here for the live errormessage:...
3
8702
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 { public:
45
2952
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 to check. What I expected was that there would be minor code bloat and some speed improvement when using templates. However... I wrote a basic list container (using templates), and a list container (using virtual derived classes). I also tried...
272
14220
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 dimensional arrays from std::vectors ??? I want to use normal Array Syntax.
10
105219
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 make use of these methods assume that the methods obey these contracts so it is necessary to ensure that if your classes override these methods, they do so correctly. In this article I'll take a look at the equals and hashCode methods. ...
32
2750
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 issue. A noddy mixin layer example should illustrate the issue... class Base { protected: int m_Field;
0
9795
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10780
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10498
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10212
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9319
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6951
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5623
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5789
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3970
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.