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

Template class member function specialization

Guys,

When I specialize a template class member function (I.e. a member
function of a template class) based on that class' type, bad things
happen. Here's some code:

---- test_header.h
#ifndef TEST_HEADER_H
#define TEST_HEADER_H

#include <iostream>

template <typename T>
class Test
{
public:
void out();
};

template <>
void
Test<char>::out()
{
std::cout << "I am a char!\n";
}

template <typename T>
void
Test<T>::out()
{
std::cout << "I am a T!\n";
}

#endif
----
---- test_main.cpp
#include "test_header.h"

int main()
{
Test<chart1;
Test<intt2;

t1.out();
t2.out();

return 0;
}
----
---- test_other.cpp
#include "test_header.h"

int test()
{
Test<chart;
Test<intt2;

t.out();
t2.out();
}
----
---- g++-4.1.2 reports:
../test_other.o: In function `Test<char>::out()':
.../test_header.h:15: multiple definition of `Test<char>::out()'
../test_main.o:../test_header.h:15: first defined here
----

But I thought I was supposed to put template functions and their
specializations in the header file. It works if I inline it, but I
don't *want* to inline it. In my real system, the function is a little
larger.

Any help from the gurus out there?

Grace be with you,
James Aguilar

Nov 9 '06 #1
4 4034

James Aguilar wrote:
Guys,

When I specialize a template class member function (I.e. a member
function of a template class) based on that class' type, bad things
happen. Here's some code:

---- test_header.h
#ifndef TEST_HEADER_H
#define TEST_HEADER_H

#include <iostream>

template <typename T>
class Test
{
public:
void out();
};

template <>
void
Test<char>::out()
{
std::cout << "I am a char!\n";
}

template <typename T>
void
Test<T>::out()
{
std::cout << "I am a T!\n";
}

#endif
----
---- test_main.cpp
#include "test_header.h"

int main()
{
Test<chart1;
Test<intt2;

t1.out();
t2.out();

return 0;
}
----
---- test_other.cpp
#include "test_header.h"

int test()
{
Test<chart;
Test<intt2;

t.out();
t2.out();
}
----
---- g++-4.1.2 reports:
./test_other.o: In function `Test<char>::out()':
../test_header.h:15: multiple definition of `Test<char>::out()'
./test_main.o:../test_header.h:15: first defined here
----

But I thought I was supposed to put template functions and their
specializations in the header file. It works if I inline it, but I
don't *want* to inline it. In my real system, the function is a little
larger.

Any help from the gurus out there?

Grace be with you,
James Aguilar
Your class Test is a template class. Your function out is not a
template member function.

So

1>Either you first need to explicity specialize your class

or

2>Make your member function "out" to be a template member function and
then specialize it.

class Test
{
public:
template <typename T>
void out();
};

template<>
void Test::Out<char>()
{
}

Nov 9 '06 #2
am******@gmail.com wrote:
>
Your class Test is a template class. Your function out is not a
template member function.

So

1>Either you first need to explicity specialize your class

or

2>Make your member function "out" to be a template member function and
then specialize it.
I certainly can't do the second. The class I'm actually writing is an
iterator over a specialized container. But I want the iterator's
operator *() function to do something special, but only when it is
templatized by char. You're saying I have to reimplement the entire
class in order to change just one method? Or am I misreading you
somehow?

Yours,
James Aguilar

Nov 9 '06 #3
James Aguilar wrote:
Guys,

When I specialize a template class member function (I.e. a member
function of a template class) based on that class' type, bad things
happen. Here's some code:

---- test_header.h
#ifndef TEST_HEADER_H
#define TEST_HEADER_H

#include <iostream>

template <typename T>
class Test
{
public:
void out();
};

template <>
What if you just drop this "tempalate <>" thing? What you're trying
to do is to define the body of a particular function. It's not
a specialisation. Of course, since you stuck it into a header you
probably want to declare it "inline" while you're at it. Or move it
into an implemenation file to avoid multiple definition errors.
void
Test<char>::out()
{
std::cout << "I am a char!\n";
}

template <typename T>
void
Test<T>::out()
{
std::cout << "I am a T!\n";
}

#endif
----
---- test_main.cpp
#include "test_header.h"

int main()
{
Test<chart1;
Test<intt2;

t1.out();
t2.out();

return 0;
}
----
---- test_other.cpp
#include "test_header.h"

int test()
{
Test<chart;
Test<intt2;

t.out();
t2.out();
}
----
---- g++-4.1.2 reports:
./test_other.o: In function `Test<char>::out()':
../test_header.h:15: multiple definition of `Test<char>::out()'
./test_main.o:../test_header.h:15: first defined here
----

But I thought I was supposed to put template functions and their
specializations in the header file. It works if I inline it, but I
don't *want* to inline it. In my real system, the function is a
little larger.
So, don't inline it. Declare it in the header (without the 'template<>')
and then define it in one of the translation units.
>
Any help from the gurus out there?
See above.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 9 '06 #4

James Aguilar wrote:
am******@gmail.com wrote:

Your class Test is a template class. Your function out is not a
template member function.

So

1>Either you first need to explicity specialize your class

or

2>Make your member function "out" to be a template member function and
then specialize it.

I certainly can't do the second. The class I'm actually writing is an
iterator over a specialized container. But I want the iterator's
operator *() function to do something special, but only when it is
templatized by char. You're saying I have to reimplement the entire
class in order to change just one method? Or am I misreading you
somehow?
then just take out the template < from the top of the specialization.
>
Yours,
James Aguilar
Nov 9 '06 #5

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

Similar topics

2
by: Jeff | last post by:
/* -------------------------------------------------------------------------- Hello, I was experimenting with class templates and specializing member functions and came across a simple problem...
3
by: Dave | last post by:
Hello all, I am trying to create a full specialization of a member function template of a class template. I get the following errors: Line 29: 'foo<T1>::bar' : illegal use of explicit...
5
by: Levent | last post by:
Hi, Why doesn't this work? (tried with gcc 3.3.3 and VC++ 7.1): #include <iostream> template<class T, unsigned N> struct Foo { void func(); }; template<class T, unsigned N>
2
by: pookiebearbottom | last post by:
Just trying to learn some things about templates. Was wondering how boost::tupple really works, but the headers were a bit confusing to me. I know you get do something like the following, just...
3
by: toton | last post by:
Hi, I want to specialize template member function of a template class . It is creating some syntax problem .... Can anyone say how to do it ? The class is something like this template<typename...
4
by: stinos | last post by:
Hi All! suppose a class having a function for outputting data somehow, class X { template< class tType > void Output( const tType& arg ) { //default ToString handles integers/doubles
8
by: mattias.nissler | last post by:
Hi! Here is a problem I ran into at work. The following example doesn't compile on gcc-4.1: struct cons_end {}; template<typename U,typename Vstruct cons { U elem; V tail;
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:
13
by: mike b | last post by:
Hello everyone, thanks in advance for your help. I'm new to C++ templates and have run into some issues using member function templates. I have a shared library containing templates that I'm...
5
by: chgans | last post by:
Hi all, I'm having difficulties with some template static member, especially when this member is a template instance, for example: ---- template<typename T> class BaseT { public: static...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.