对可变模板多继承函数调用的访问不稳定

Ambigious access on variadic template multi inheritence function call

本文关键字:函数调用 访问 不稳定 继承      更新时间:2023-10-16

因此,我正在研究如何解决"寻找设计模式以减少虚拟方法过载"中的问题

我的想法是使用可变模板来描述特定类可以接受哪些类型。这可能有一个小用例,但我喜欢玩模板。。。

以下是我(到目前为止)的想法:

struct Struct
{
    virtual void doit() = 0;
};
struct StructA : Struct
{
    void doit() { std::cout << "A" << std::endl; }
};
struct StructB : Struct
{
    void doit() { std::cout << "B" << std::endl; }
};
struct StructC : Struct
{
    void doit() { std::cout << "C" << std::endl; }
};
template <typename Type>
struct Accepter
{
    void accept(const Type& t) { t.doit(); };
};
template <typename...Types>
struct MultiAccepter : Accepter<Types>... {};

当只将一个类型传递给MultiAccepter时,一切都会按其应有的方式进行。只有当我传递了2个或多个模板参数类型时,问题才会出现。看起来编译器失去了在不同类型之间进行区分的能力。

int main()
{
    StructA struct_a;
    StructB struct_b;
    Accepter<StructA>                           accept_a;
    Accepter<StructB>                           accept_b;
    MultiAccepter<StructA>                      accept_multi_a;
    MultiAccepter<StructB>                      accept_multi_b;
    MultiAccepter<StructA, StructB>             accept_multi_ab;

    accept_a.accept(struct_a);              //OK
    accept_b.accept(struct_b);              //OK
    accept_multi_a.accept(struct_a);        //OK
    accept_multi_b.accept(struct_b);        //OK
    accept_multi_ab.accept(struct_a);       //NOK:
                                            //  error C2385: ambiguous access of 'accept'
                                            //      note : could be the 'accept' in base 'Accepter<StructA>'
                                            //      note : or could be the 'accept' in base 'Accepter<StructB>'
    accept_multi_ab.accept(struct_b);       //NOK:
                                            //  error C2385: ambiguous access of 'accept'
                                            //      note : could be the 'accept' in base 'Accepter<StructA>'
                                            //      note : or could be the 'accept' in base 'Accepter<StructB>'
                                            //  error C2664 : 'void Accepter<StructA>::accept(const Type &)' : cannot convert argument 1 from 'StructB' to 'const StructA &'
                                            //      with
                                            //      [
                                            //          Type = StructA
                                            //      ]
                                            //      note : Reason : cannot convert from 'StructB' to 'const StructA'
                                            //      note : No user - defined - conversion operator available that can perform this conversion, or the operator cannot be called
    return 0;
}

尝试使用gcc 5.2编译代码也不起作用:http://goo.gl/oVLHT8

我想这只是一个简单的问题,但我就是找不到解决办法。有人知道我做错了什么吗?


附言:这一侧的示例描述了类似的模式:http://natsys-lab.blogspot.de/2013/07/c-variadic-templates-and-multiple.html


更新:看起来基类Accepter<StructB>定义了一个函数void accept(const StructA&)。我仍然不明白为什么会这样。

我使用这里描述的模式来创建一个解决方案:https://stackoverflow.com/a/28349054/1149664

诀窍是一次继承一个基类Accepter<T0>,然后将其余类型进一步推到继承链的上游。我还将方法设置为const以使其一致。你可以根据自己的需要随意调整。

#include "stdafx.h"
#include <functional>
#include <chrono>
#include <iostream>

struct Struct
{
    virtual void doit() const = 0;
};
struct StructA : public Struct
{
    void doit() const { std::cout << "A" << std::endl; }
};
struct StructB : public Struct
{
    void doit() const { std::cout << "B" << std::endl; }
};
struct StructC : public Struct
{
    void doit() const { std::cout << "C" << std::endl; }
};
template <typename Type>
struct Accepter
{
    void accept(const Type& t) const { t.doit(); } ;
};
template <typename... Types>
struct MultiAccepter;
template <typename T0, typename...Types>
struct MultiAccepter<T0, Types...> : public Accepter<T0>, public MultiAccepter<Types...> {
    using Accepter<T0>::accept;
    using MultiAccepter<Types...>::accept;
};
template <typename T0>
struct MultiAccepter<T0> : public Accepter<T0> {
    using Accepter<T0>::accept;
};
int main()
{
    StructA struct_a;
    StructB struct_b;
    Accepter<StructA>                           accept_a;
    Accepter<StructB>                           accept_b;
    MultiAccepter<StructA>                      accept_multi_a;
    MultiAccepter<StructB>                      accept_multi_b;
    MultiAccepter<StructA, StructB>             accept_multi_ab;
    accept_a.accept(struct_a);              
    accept_b.accept(struct_b);             
    accept_multi_a.accept(struct_a);       
    accept_multi_b.accept(struct_b);     
    accept_multi_ab.accept(struct_a);    
    accept_multi_ab.accept(struct_b);     
    return 0;
}