I am testing the solution on the issue of specialized template classes .
This compiled code is excellent in g ++, but it causes linker errors when compiling with gcc. What is the cause of these errors?
$ g++ traits2.cpp
$ gcc traits2.cpp
/tmp/ccI7CNCY.o: In function `__static_initialization_and_destruction_0(int, int)':
traits2.cpp:(.text+0x36): undefined reference to `std::ios_base::Init::Init()'
traits2.cpp:(.text+0x3b): undefined reference to `std::ios_base::Init::~Init()'
/tmp/ccI7CNCY.o:(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
The traits2.ccp file contains the above solution with the emtpy main () function:
#include <iostream>
using namespace std;
template<class T> struct Traits
{
};
template<typename T> Traits<T> GetTraits(const T&)
{
return Traits<T>();
}
template <int major, int minor> struct A
{
void f()
{
cout << major << endl;
}
};
template<int N1, int N2> struct Traits<A<N1, N2> >
{
enum { major = N1, minor = N2 };
};
template <> struct A<4,0>
{
void f()
{
cout << "Specialized:" << GetTraits(*this).major << endl;
}
};
int main(int argc, char * argv[] )
{
return 1;
}
source
share