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

Problems with Template Specialization

Hi,
For a while I have had some problem understanding just how template
specialisation works in certain cases. In abridged form my code looks
like this;

--MyTemplate.h--
#ifndef MyTemplateH
#define MyTemplateH

template <class Type>
class MyTemplate
{
public:
void MyMethod(void);
};

#include "MyTemplate.inl"

#endif

--MyTemplate.inl--
#include <iostream>
#include <string>

template <class Type>
void MyTemplate<Type>::MyMethod(void)
{
std::cout << "MyTemplate<Type>::MyMethod" << std::endl;
}

template<>
void
MyTemplate<bool>::MyMethod
(void)
{
std::cout << "MyTemplate<bool>::MyMethod" << std::endl;
}

--main.cpp--
#include <iostream>
#include "MyTemplate.h"
//#include "MyClass.h"

using namespace std;

int main(void)
{
cout << "Template Specialisation Test" << endl;

MyTemplate<double> myDoubleTemplate;
MyTemplate<bool> myBoolTemplate;

myDoubleTemplate.MyMethod();
myBoolTemplate.MyMethod();

//MyClass myClass;
//myClass.MyOtherMethod();
}

If I compile and run this (gcc on cygwin);
g++ main.cpp
../a

I get exactly what I expect:
Template Specialisation Test
MyTemplate<Type>::MyMethod
MyTemplate<bool>::MyMethod

However, if I uncomment the MyClass stuff and compile in the relevant
files;

--MyClass.h--
#ifndef MyClassH
#define MyClassH

class MyClass
{
public:
void MyOtherMethod(void);
};

#endif

--MyClass.cpp--
#include "MyClass.h"
#include "MyTemplate.h"
#include <iostream>

void MyClass::MyOtherMethod(void)
{
std::cout << "MyClass::MyOtherMethod" << std::endl;

MyTemplate<double> myClassDoubleTemplate;
myClassDoubleTemplate.MyMethod();

MyTemplate<bool> myClassBoolTemplate;
myClassBoolTemplate.MyMethod();
}

I get this;

$ g++ main.cpp MyClass.cpp
/cygdrive/c/DOCUME~1/sjs1/LOCALS~1/Temp/cc4Op3z4.o(.text+0x0):MyClass.cpp:
multi
ple definition of `MyTemplate<bool>::MyMethod()'
/cygdrive/c/DOCUME~1/sjs1/LOCALS~1/Temp/ccPG3TEU.o(.text+0x0):main.cpp:
first de
fined here
collect2: ld returned 1 exit status

Now, I can understand this, given that the .inl file is included in
both places - but I have tried moving stuff the specialisation or the
template method or both into seperate .cpp files, with absolutely no
success; any ideas how to resolve this?

Stephen.

Sep 2 '05 #1
2 1428

Stephen Starkie wrote:
[snip]

template <class Type>
void MyTemplate<Type>::MyMethod(void)
{
std::cout << "MyTemplate<Type>::MyMethod" << std::endl;
}

template<>
void
MyTemplate<bool>::MyMethod
(void)
{
std::cout << "MyTemplate<bool>::MyMethod" << std::endl;
}

[snip]

$ g++ main.cpp MyClass.cpp
/cygdrive/c/DOCUME~1/sjs1/LOCALS~1/Temp/cc4Op3z4.o(.text+0x0):MyClass.cpp:
multi
ple definition of `MyTemplate<bool>::MyMethod()'
/cygdrive/c/DOCUME~1/sjs1/LOCALS~1/Temp/ccPG3TEU.o(.text+0x0):main.cpp:
first de
fined here
collect2: ld returned 1 exit status

You can try this, it should work (use the 'inline' keyword):

template<>
inline void
MyTemplate<bool>::MyMethod
(void)
{
std::cout << "MyTemplate<bool>::MyMethod" << std::endl;

}

Hth, -shez-

Sep 2 '05 #2
Thankyou; esp. so quickly

I feel like an idiot; I should have known this! Sometimes it just
takes someone to look over your shoulder!

Stephen

Sep 2 '05 #3

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

Similar topics

17
by: Paul MG | last post by:
Hi Template partial specialization always seems like a fairly straightforward concept - until I try to do it :). I am trying to implement the input sequence type (from Stroustrup section...
2
by: SainTiss | last post by:
Hi, If you've got a template class with lots of methods, and then you've got a type which works with the template, except for one method... What you need to do there is specialize the...
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...
2
by: Jeff | last post by:
/* -------------------------------------------------------------------------- Hello, I was experimenting with class templates and specializing member functions and came across a simple problem...
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...
2
by: Yang Zhang | last post by:
I have a small program like this: //////////////////////////////////////////////// #include <iostream> using namespace std ; // set bits in an address template <typename T> inline T*...
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...
2
by: Thomas Kowalski | last post by:
Hi, I would like to write a template class Polygon<VertexTypthere vertex typ can be eigther a pointer or a value typ. It has an attribute: std::vector<VertexTypvertices; And a methode:...
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: 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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
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...
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
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,...

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.