views:

84

answers:

3

I have a templated class that performs an action on the class that is given as template argument. For some of my classes I want to 'group' the functionality in one class, to make it easier for the caller. In fact the code looks something like this (names were changed):

template<typename T>
class DoSomeProcessing
{
public:
   process(T &t);
};

class ProcessingFrontEnd : public DoSomeProcessing<CustomerOrder>, public DoSomeProcessing<ProductionOrder>
{
};

The problem is that when I call ProcessingFrontEnd::process with a CustomerOrder as argument, that the compiler complains about it.

I tried to reproduce the problem in a smaller test application. This is the code:

#include <vector>

class X : public std::vector<char>
        , public std::vector<void *>
{
};

int main(void)
{
X x;
x.push_back('c');
return 0;
}

And indeed, if this is compiled, Microsoft's VS2010 compiler gives this error:

test.cpp
test.cpp(11) : error C2385: ambiguous access of 'push_back'
        could be the 'push_back' in base 'std::vector<char,std::allocator<char> >'
        or could be the 'push_back' in base 'std::vector<void *,std::allocator<void *> >'
test.cpp(11) : error C3861: 'push_back': identifier not found

I tested this test application with different types (char+void*, double+void*) and different arguments in the call ('c', 3.14), but the error message is always the same.

I tested this with VS2005 and VS2010 but I always get the same error.

Why can't the compiler determine the correct function to call? What makes this confusing for the compiler? Or is it just a bug in the Microsoft compiler?

EDIT: If I explicitly add 2 push_back methods to my class, like this:

class X : public std::vector<char>
        , public std::vector<void *>
{
public:
   void push_back(char c) {}
   void push_back(void *p) {}
};

The compiler doesn't complain anymore. So with these methods he can clearly distinguish between a character and a void-pointer. Why can't he do this if the two push_back methods are inherited from the parent?

A: 

How is the compiler supposed to know which process you want to call? There's two options. Do you want both, one, or the other?

You need to override process in the derived class.

DeadMG
The compiler knows this from the template types. The first parent class in the example (std::vector<char>) has a method push_back(char), while the second parent class (std::vector<void*>) has a method push_back(void*). Why is this different from having the two push_back methods in my class itself?
Patrick
Looks similar to "name hiding"- but but basic.scope.hiding in the C++ standard doesn't apply to this case. Walking through basic.lookup might turn up an explicit or implicit rule - which might still not give you a rationale.
peterchen
+1  A: 

I believe you are running afoul of the C++ overloading rules which prohibit overloading across classes. You'd get the same results if your template classes were two separate classes, each with its own process(CustomerOrder) and process(ProductionOrder) member.

The workaround is explicit using statements inside your derived class, pulling in each overload from each of the template base classes.

Pontus Gagge
+3  A: 

This is by design. The compiler is not trying to resolve overloaded functions because these are not overloaded functions. The standard is really clear on that (see 10.2.2). If the same name is found in two different bases, it's an ambiguity, even if they could be resolved correctly with the call (i.e. in your case). Same-named functions in different classes will typically have quite different purposes and hence the selection between them should not be made on the basis of their arguments. There are many good reasons not to allow that, but here's one.

Imagine your class C derives from A and B and these two base classes come from two different libraries. If the author of B adds a new function to the class, it may break the user's code by redirecting a call from A::foo() to B::foo() if the latter is a better match.

If you want the two functions to be treated in the same way that they would be if part of a single class, then the best way to do it is with using declarations in the derived class. Just add

using std::vector<char>::push_back;
using std::vector<void *>::push_back;

to the declaration of class X.

David
Yep, that's it. Apparently, this is to prevent user errors, rather than something the compiler cannot resolve. Adding the using clauses indeed solves the problem. Thanks.
Patrick