J
James Aguilar
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
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