C ++ Partial Ordering Fails with 2 template parameters

So, the following template functions with partial specialization are given

template<typename T>
void foo(vector<T> &in) {
    cout << "vector" << endl;
}

template<typename T>
void foo(T &in) {
    cout << "scalar" << endl;
}

int main(int arc, char *argv[]) {
    vector<double> a;
    double b;

    foo(a);
    foo(b);
    return 0;
}

I have no problem compiling with g ++ 3.4.6 and getting the expected result:

vector
scalar

Now, if I add a second template parameter:

template<class U, typename T>
void foo2(vector<T> &in) {
    U a;
    cout << "vector" << endl;
}

template<class U, typename T>
void foo2(T &in) {
    U a;
    cout << "scalar" << endl;
}

and call it like this:

int main(int arc, char *argv[]) {
    vector<double> a;
    double b;

    foo2<double>(a);
    foo2<double>(b);
    return 0;
}

When I try to compile it, GCC 3.4.6 gives me an ambiguous overload error.

error: call of overloaded `foo2(std::vector<double, std::allocator<double> >&)' is ambiguous
note: candidates are: void foo2(std::vector<T, std::allocator<_T2> >&) [with U = double, T = double]
note:                 void foo2(T&) [with U = double, T = std::vector<double, std::allocator<double> >]

I do not see how the second template parameter now makes overloading ambiguous. As far as I can tell, the vector version should still be more specialized. Is this just a bug in 3.4? Is there a workaround?

For writing, the code works in gcc 4.1 without problems. Unfortunately, some of our tools are still 3.4 related, so updating is not a solution.

Thank.

+3
1

, fixed . :

template<typename U>
struct foo2 {
    template<typename T>
    void operator()( std::vector<T> &in ) {
        U a;
        cout << "vector" << endl;
    }
    template<typename T>
    void operator()( T& in ) {
        U a;
        cout << "scalar" << endl;
    }
};

int main(int arc, char *argv[]) {
    vector<double> a;
    double b;

    foo2<double>()(a);
    foo2<double>()(b);
    return 0;
}
+2

Source: https://habr.com/ru/post/1751053/


All Articles