Inside the C ++ template function foo (), calling to :: bar (TT *) gives the following error in gcc 4.4.3:
g++ -o hello.o -c -g hello.cpp
hello.cpp: In function 'void foo(std::vector<TT*, std::allocator<TT*> >&)':
hello.cpp:8: error: '::bar' has not been declared
Here's the abusive code:
#include <vector>
template<typename TT> void foo(std::vector<TT*> &vec)
{
TT *tt;
::bar(tt);
vec.push_back(tt);
}
class Blah
{
};
void bar(Blah *&)
{
}
int main(int argc, char *argv[])
{
std::vector<Blah*> vec;
foo(vec);
return 0;
}
C ++ distinguishes between characters that depend on the template parameter (here, here), and those characters that are independent and can be immediately evaluated.
Obviously, the compiler believes that the call to my :: bar (TT *) is independent and tries to solve it immediately. Similarly, this function callsdepending on TT, because the function call takes a parameter of type TT *, so the compiler must wait until the instance foo (vec) resolves :: bar (TT *).
Is this a gcc bug or am I missing something subtle in C ++ templates?
EDIT: :: bar(), , . , main() TT = Blah TT = Argh. 35 28 ( -). line 8.
# 2: .
EDIT # 3: , . (tt) (Blah *). , . ( ).
#include <vector>
class XX {};
void bar(XX*) {}
class CC {
public:
void bar();
void bar(int *);
void bar(float *);
template<typename TT> static void foo(std::vector<TT*> &vec);
};
template<typename TT>
void CC::foo(std::vector<TT*> &vec) {
using ::bar;
TT *tt;
bar(tt);
vec.push_back(tt);
}
class Argh {};
void bar(Argh *&aa) { aa = new Argh; }
class Blah {};
void bar(Blah *&bb) { bb = new Blah; }
int main(int argc, char *argv[]) {
std::vector<Blah*> vec;
CC::foo(vec);
return 0;
}