5

我有一个模板类,它对作为模板参数给出的类执行操作。对于我的一些课程,我想将功能“分组”到一个课程中,以使调用者更容易。实际上,代码看起来像这样(名称已更改):

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

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

问题是,当我使用 CustomerOrder 作为参数调用 ProcessingFrontEnd::process 时,编译器会抱怨它。

我试图在一个较小的测试应用程序中重现该问题。这是代码:

#include <vector>

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

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

事实上,如果这是编译的,微软的 VS2010 编译器会给出这个错误:

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

我在调用中使用不同类型(char+void*、double+void*)和不同参数('c'、3.14)测试了这个测试应用程序,但错误消息始终相同。

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?

4

3 回答 3

4

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.

于 2010-08-24T11:45:20.803 回答
2

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.

于 2010-08-24T11:45:06.407 回答
-2

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.

于 2010-08-24T11:11:52.090 回答