c++ - 对可变参数模板多继承函数调用的访问不明确

标签 c++ templates inheritance c++14 multiple-inheritance

<分区>

所以,我正在研究如何解决 Looking for design pattern to reduce virtual method overloads 中的问题。

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

这是我想出的(到目前为止):

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;
}

关于c++ - 对可变参数模板多继承函数调用的访问不明确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33615169/

相关文章:

c++ - 如何将python dict发送到C++

c++ - QT 用比 SLOT 更少的参数连接 SIGNAL

c++ - 运行时错误 - 变量 arr_processes 周围的堆栈已损坏

c++ - 基类和派生类 C++

java - 访问父类(super class)对象 vector 中的子类方法

c++ - Boost.MPI/Boost.Interprocess - 如何检测进程是否在同一台机器上运行?

c++ - 变量模板偏特化和 constexpr

c++ - Haskell 等同于非类型上的 C++ 模板

c++ - C++获取构造函数的类型

php - 创建一个新的被调用类而不是父类的实例