Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Template class member function specialization

Reply
Thread Tools

Template class member function specialization

 
 
James Aguilar
Guest
Posts: n/a
 
      11-09-2006
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>:ut()
{
std::cout << "I am a char!\n";
}

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

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

int main()
{
Test<char> t1;
Test<int> t2;

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

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

int test()
{
Test<char> t;
Test<int> t2;

t.out();
t2.out();
}
----
---- g++-4.1.2 reports:
../test_other.o: In function `Test<char>:ut()':
.../test_header.h:15: multiple definition of `Test<char>:ut()'
../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

 
Reply With Quote
 
 
 
 
amparikh@gmail.com
Guest
Posts: n/a
 
      11-09-2006

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>:ut()
> {
> std::cout << "I am a char!\n";
> }
>
> template <typename T>
> void
> Test<T>:ut()
> {
> std::cout << "I am a T!\n";
> }
>
> #endif
> ----
> ---- test_main.cpp
> #include "test_header.h"
>
> int main()
> {
> Test<char> t1;
> Test<int> t2;
>
> t1.out();
> t2.out();
>
> return 0;
> }
> ----
> ---- test_other.cpp
> #include "test_header.h"
>
> int test()
> {
> Test<char> t;
> Test<int> t2;
>
> t.out();
> t2.out();
> }
> ----
> ---- g++-4.1.2 reports:
> ./test_other.o: In function `Test<char>:ut()':
> ../test_header.h:15: multiple definition of `Test<char>:ut()'
> ./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>()
{
}

 
Reply With Quote
 
 
 
 
James Aguilar
Guest
Posts: n/a
 
      11-09-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      11-09-2006
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>:ut()
> {
> std::cout << "I am a char!\n";
> }
>
> template <typename T>
> void
> Test<T>:ut()
> {
> std::cout << "I am a T!\n";
> }
>
> #endif
> ----
> ---- test_main.cpp
> #include "test_header.h"
>
> int main()
> {
> Test<char> t1;
> Test<int> t2;
>
> t1.out();
> t2.out();
>
> return 0;
> }
> ----
> ---- test_other.cpp
> #include "test_header.h"
>
> int test()
> {
> Test<char> t;
> Test<int> t2;
>
> t.out();
> t2.out();
> }
> ----
> ---- g++-4.1.2 reports:
> ./test_other.o: In function `Test<char>:ut()':
> ../test_header.h:15: multiple definition of `Test<char>:ut()'
> ./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


 
Reply With Quote
 
amparikh@gmail.com
Guest
Posts: n/a
 
      11-09-2006

James Aguilar wrote:
> (E-Mail Removed) 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Full specialization of a member function template of a class template Dave C++ 4 06-04-2010 12:15 PM
Specialization of member function template in template class? Joseph Turian C++ 4 03-20-2006 10:07 AM
parse error in gcc but success in vc.net, call a non_template class's template member function from a template class's member function! ken C++ 2 06-28-2005 06:57 AM
Out-of-class-template member template specialization syntax BigMan C++ 1 03-26-2005 02:23 PM
Member template function specialization in a template class Ruben Campos C++ 3 02-15-2005 10:40 AM



Advertisments