Here is the example program:
==========
// test.cxx
#include <algorithm>
#include <map>
namespace {
template <class T>
struct transformValue {
size_t operator()(const T &x) const
{
return x + 10;
}
};
}
extern std::map<int, int, transformValue<int> > *test;
std::map<int, int, transformValue<int> > *test;
==========
If I compile this file with g++ 4.2 by the following command:
g++ -c test.cxx
and then use this command to check symbol:
nm test.o
I cannot find the global varible `test' in symbol table:
U _ZNKSs4sizeEv
U _ZNKSsixEj
00000000 t _ZSt17__verify_groupingPKcjRKSs
00000000 W _ZSt3minIjERKT_S2_S2_
U __gxx_personality_v0
I can find the symbol `test' by using older version g++, such as g++ 4.1.x or
3.x:
00000000 r _ZN9__gnu_cxx16__stl_prime_listE
U _ZNKSs4sizeEv
U _ZNKSsixEj
00000000 t _ZSt17__verify_groupingPKcjRKSs
00000000 W _ZSt3minIjERKT_S2_S2_
U __gxx_personality_v0
00000000 B test
The keypoints of this problem are:
1. struct transformValue<> is defined in anonymous namepsace
2. transformValue<int> is passed to one of the template argument of std::map<>
for instantiating it
3. declare the global variable (should be a pointer) `test' as a external
linkage object
4. define the global varible `test' (should be a pointer)
If the variable `test' isn't declared/defined as pointer type, such as:
extern std::map<int, int, transformValue<int> > test;
std::map<int, int, transformValue<int> > test;
You can find a similar problem: the symbol `test' will become a `static' local
variable.
It can be observed by using the tool `nm':
00000000 b test
Its attribute must be `B', but became to `b'.