在 C++ 中为抽象类模板创建接口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3251549/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 12:26:59  来源:igfitidea点击:

Creating an interface for an abstract class template in C++

c++inheritancetemplatespointersinterface

提问by bishboshbash

I have the code as below. I have a abstract template class Foo and two subclasses (Foo1 and Foo2) which derive from instantiations of the template. I wish to use pointers in my program that can point to either objects of type Foo1 or Foo2, hence I created an interface IFoo.

我有如下代码。我有一个抽象模板类 Foo 和两个从模板实例派生的子类(Foo1 和 Foo2)。我希望在我的程序中使用可以指向 Foo1 或 Foo2 类型对象的指针,因此我创建了一个接口 IFoo。

My problem is I'm not sure how to include functionB in the interface, since it is dependant on the template instantiation. Is it even possible to make functionB accessible via the interface, or am I attempting the impossible?

我的问题是我不确定如何在接口中包含 functionB,因为它依赖于模板实例化。甚至可以通过接口访问 functionB,还是我在尝试不可能的事情?

Thank you very much for your help.

非常感谢您的帮助。

class IFoo {
    public:
        virtual functionA()=0;

};

template<class T>
class Foo : public IFoo{
    public:
        functionA(){ do something; };
        functionB(T arg){ do something; };
};

class Foo1 : public Foo<int>{
...
};

class Foo2 : public Foo<double>{
...
};

回答by Matthieu M.

You are actually attempting the impossible.

你实际上是在尝试不可能的事情。

The very heart of the matter is simple: virtualand templatedo not mix well.

事情的非常心脏很简单:virtualtemplate不拌匀。

  • templateis about compile-time code generation. You can think of it as some kind of type-aware macros + a few sprinkled tricks for meta programming.
  • virtualis about runtime decision, and this require some work.
  • template是关于编译时代码生成的。您可以将其视为某种类型感知宏 + 一些用于元编程的技巧。
  • virtual是关于运行时决策,这需要一些工作。

virtualis usually implemented using a virtual tables (think of a table which lists the methods). The number of methods need be known at compile time and is defined in the base class.

virtual通常使用虚拟表来实现(想想列出方法的表)。方法的数量需要在编译时知道并在基类中定义。

However, with your requirement, we would need a virtual table of infinite size, containing methods for types we haven't seen yet and that will only be defined in the years to come... it's unfortunately impossible.

但是,根据您的要求,我们需要一个无限大小的虚拟表,其中包含我们尚未见过的类型的方法,并且只会在未来几年内定义......不幸的是,这是不可能的。

And if it were possible ?

如果可能的话?

Well, it just would not make sense. What happens when I call Foo2with an int? It's not meant for it! Therefore it breaks the principle that Foo2implements all the methods from IFoo.

好吧,这没有任何意义。当我打电话,会发生什么Foo2int?不是为了它!因此它打破了Foo2实现所有方法的原则IFoo

So, it would be better if you stated the real problem, this way we could help you at a design level rather than at a technical level :)

所以,如果你说出真正的问题会更好,这样我们可以在设计层面而不是技术层面为你提供帮助:)

回答by Igor Zevaka

Easiest way is to make your interface templated.

最简单的方法是使您的界面模板化。

template <class T>
class IFoo {
    public:
        virtual void functionA()=0;
        virtual void functionB(T arg){ do something; };
};

template<class T>
class Foo : public IFoo<T>{
    public:
        void functionA(){ do something; };
        void functionB(T arg){ do something; };
};

回答by Nordic Mainframe

Since functionB's argument type must be known in advance, you have only one choice: Make it a type which can hold every possible argument. This is sometimes called a "top type" and the boost libraries have the anytype which gets quite close to what a top type would do. Here is what could work:

由于必须事先知道 functionB 的参数类型,因此您只有一个选择:使其成为可以容纳所有可能参数的类型。这有时被称为“顶级类型”,boost 库的any类型与顶级类型非常接近。这是可行的:

#include <boost/any.hpp>
#include <iostream>
using namespace boost;

class IFoo {
    public:
    virtual void functionA()=0;
    virtual void functionB(any arg)=0; //<-can hold almost everything
};

template<class T>
class Foo : public IFoo{
    public:
        void functionA(){  };
        void real_functionB(T arg)
        {
         std::cout << arg << std::endl;
        };
        // call the real functionB with the actual value in arg
        // if there is no T in arg, an exception is thrown!

        virtual void functionB(any arg)
        {
            real_functionB(any_cast<T>(arg));
        }
};

int main()
{
    Foo<int> f_int;
    IFoo &if_int=f_int;

    if_int.functionB(10);

    Foo<double> f_double;
    IFoo &if_double=f_double;
if_int.functionB(10.0);

}

Unfortunately, any_castdoes not know about the usual conversions. For example any_cast<double>(any(123))throws an exception, because it does not even try to convert the integer 123 to a double. If does not care about conversions, because it is impossible to replicate all of them anyway. So there are a couple of limitations, but it is possible to find workarounds if necessary.

不幸的是,any_cast不知道通常的转换。例如any_cast<double>(any(123))抛出异常,因为它甚至不会尝试将整数 123 转换为双精度数。如果不关心转换,因为无论如何都不可能复制所有转换。因此存在一些限制,但如有必要,可以找到解决方法。

回答by Karmastan

I don't think you can get what you want. Think of this if you were to implement your suggestion: if you have a pointer to an IFooinstance and you call functionB(), what type parameter should you give it? The underlying problem is that Foo1::functionBand Foo2::functionBhave different signatures and do different things.

我不认为你能得到你想要的。如果您要实现您的建议,请考虑这一点:如果您有一个指向IFoo实例的指针并且您调用functionB(),您应该给它什么类型参数?潜在的问题是Foo1::functionBFoo2::functionB具有不同的签名并做不同的事情。

回答by Martin Hennings

You can achieve something comparable by wrapping the IFoo* pointer in a class and exposing the functionality via generic template functions of the non-templated wrapper class:

您可以通过将 IFoo* 指针包装在一个类中并通过非模板化包装类的通用模板函数公开功能来实现类似的东西:

#include <assert.h>

// interface class
class IFoo {
public:
    virtual int type() const = 0; // return an identifier for the template parameter
    virtual bool functionA() = 0;
};

// This function returns a unique identifier for each supported T
template <typename T> static int TypeT() { static_assert("not specialized yet"); }
template <> static int TypeT<bool>() { return 0; }
template <> static int TypeT<double>() { return 1; }
//template <> static int TypeT<...>() { ... }

// templated class
template <typename T> class FooT : public IFoo {
public:
    int type() const override { return TypeT<T>(); }

    bool functionA() override { return true; }

    // not in interface
    bool functionB(T arg) { return arg == T(); }
};

// function to create an instance of FooT (could also be static function in FooT)
static IFoo* CreateFooT(int type)
{
    switch (type)
    {
    case 0: return new FooT<bool>();
    case 1: return new FooT<double>();
    //case ...: return new FooT<...>();
    default: return nullptr;
    }
}


// Non-templated wrapper class
class FooWrapper {
private:
    IFoo *pFoo;
public:
    FooWrapper(int type) : pFoo(CreateFooT(type)) { assert(pFoo != nullptr); }
    ~FooWrapper() { delete pFoo; }

    bool functionA() { return pFoo->functionA(); }

    template <typename T> bool functionB(T arg)
    {
        if(pFoo->type() != TypeT<T>())
        {
            assert(pFoo->type() == TypeT<T>());
            return false;
        }
        return static_cast<typename FooT<T>*>(pFoo)->functionB(arg);
    }



    // fun stuff:
    // (const pendants omitted for readability)

    bool changeType(int type)
    {
        delete pFoo;
        pFoo = CreateFooT(type);
        return pFoo != nullptr;
    }

    IFoo* Interface() { return pFoo; }

    IFoo* operator->() { return pFoo; }

    operator IFoo&() { return *pFoo; }

    template <typename T> FooT<T> *InterfaceT()
    {
        if(pFoo->type() != TypeT<T>())
        {
            assert(pFoo->type() == TypeT<T>());
            return nullptr;
        }
        return static_cast<typename FooT<T>*>(pFoo);
    }
};

int main(int argc, char *argv[])
{
    FooWrapper w1(TypeT<bool>());
    FooWrapper w2(TypeT<double>());

    w1.functionA(); // ok
    w2.functionA(); // ok

    w1.functionB(true); // ok
    w1.functionB(0.5); // runtime error!

    w2.functionB(true); // runtime error!
    w2.functionB(0.5); // ok


    // fun stuff
    w2.changeType(TypeT<bool>()); // older changes will be lost
    w2.functionB(true); // -> now ok

    w1.Interface()->functionA();
    w1->functionA();

    IFoo &iref = w1;
    iref.functionA();

    FooT<bool> *ref = w1.InterfaceT<bool>();
    ref->functionB(true);

    return 0;
}

It is of course your responsibility to call the functions with the correct types, but you can easily add some error handling.

使用正确的类型调用函数当然是您的责任,但您可以轻松添加一些错误处理。