Шаблоны функций перегрузки в пространствах имен

Why does this fail to compile with GCC 4.4?

template<typename T>
class A {
public:
    void foo () {

    }

private:
    T x;
};

namespace Ns {
template<typename T>
void do_it (A<T> a) {
    a.foo ();
}
};

template<typename T>
void myfun (T x) {
    Ns::do_it (x);
}

template<typename T>
class B {
public:
    void bar () {

    }

private:
    T x;
};

namespace Ns {
template<typename T>
void do_it (B<T> b) {
    b.bar ();
}
};

int main () {
    A<int> a;
    B<int> b;

    myfun (a);
    myfun (b); // error: no matching function call to do_it(B<int>&)

    return 0;
}

It must have something to do with the namespace of do_it. When I remove the namespace around it, it compiles.

Background: I am building a set of functions that may be used with many different container classes. To handle the different interfaces uniformly I use freestanding functions that are overloaded for each of the container classes. These functions shall be put into a namespace to avoid cluttering the global namespace with them.

The definitions for B shall be thought of as coming from a different header file than those for A so reordering is not an option.

8
задан Roland W 18 April 2011 в 17:31
поделиться