My code contains two parts - a class template test
inside namespace my
, and a function frobnicate
in a global namespace. The class template wants to use this function in one of its methods, and I want to declare it near its use:
namespace my
{
template <class T>
struct test
{
void do_stuff(T& i)
{
void frobnicate(T&);
frobnicate(i);
// more code here
}
};
}
struct SomeClass {};
void frobnicate(SomeClass& i)
{
// some code here
}
int main()
{
my::test<SomeClass> t;
SomeClass object;
t.do_stuff(object);
}
This works in MS Visual Studio 2017 due to a bug (described here), because Visual Studio declares the function in the global namespace, while according to the Standard, such declaration declares the function in current namespace.
However, gcc rightfully complains:
... undefined reference to `my::frobnicate(SomeClass&)'
Can I make it work in gcc or any other standard-compliant compiler?
If I put the declaration of frobnicate
before declaring template test
, the code works. However, in my real code, these two parts of code are in unrelated header-files, and I want my code to work regardless of the order of #include
s, if possible.