I didn't read the relevant thread, but I'm fairly certain this will not work to do what you want. (I don't even really care what you want to do.) It has to do with the way compilers and linkers handle templates and the mysterious "anonymous" 'namespace'. Unfortunately, it is neither undefined nor dependable. It just happens that compilers and linkers don't manage everything correctly. Worse, what you get depends on the order various files are compiled/linked.
Trust me on this: you want to compile the following code chunks as separate files, link them in various orders, and then execute them. The results aren't correct and consistent on any compiler suite I've used--and I've used a lot. Yes, "test_a.cxx" and "test_b.cxx" are nearly identical, but that's the point.
Soma
(tester.cxx):
Code:
void test_a();
void test_b();
int main()
{
test_a();
test_b();
return(0);
}
(test_a.cxx):
Code:
#include <iostream>
#if defined(__GNUC__)
#include <cxxabi.h>
#endif
template
<
typename type_F
>
struct base
{
virtual void go() = 0;
};
namespace
{
template
<
typename type_F
>
struct derived: public base<type_F>
{
virtual void go()
{
std::cout << __FILE__ << '\n';
}
};
}
template
<
typename type_F
>
struct holder
{
holder()
{
p = new derived<type_F>;
}
~holder()
{
delete p;
}
void go()
{
#if defined(__GNUC__)
int status(0);
std::cout << abi::__cxa_demangle(typeid(*p).name(), 0, 0, &status) << ':';
#endif
#if !defined(__GNUC__)
std::cout << typeid(*p).name() << ':';
#endif
p->go();
}
base<type_F> * p;
};
void test_a()
{
std::cout << "Test A:";
holder<int> h;
h.go();
}
(test_b.cxx):
Code:
#include <iostream>
#if defined(__GNUC__)
#include <cxxabi.h>
#endif
template
<
typename type_F
>
struct base
{
virtual void go() = 0;
};
namespace
{
template
<
typename type_F
>
struct derived: public base<type_F>
{
virtual void go()
{
std::cout << __FILE__ << '\n';
}
};
}
template
<
typename type_F
>
struct holder
{
holder()
{
p = new derived<type_F>;
}
~holder()
{
delete p;
}
void go()
{
#if defined(__GNUC__)
int status(0);
std::cout << abi::__cxa_demangle(typeid(*p).name(), 0, 0, &status) << ':';
#endif
#if !defined(__GNUC__)
std::cout << typeid(*p).name() << ':';
#endif
p->go();
}
base<type_F> * p;
};
void test_b()
{
std::cout << "Test B:";
holder<int> h;
h.go();
}