c++ - 具有模板化函数的类的接口(interface)类

标签 c++ templates

class Foo1: public IFoo
{
public:
    template <class T>
    std::vector<T> foo()
    {
        return std::vector<T>();
    }
};

class Foo2: public IFoo
{
public:
    template <class T>
    std::vector<T> foo()
    {
        return std::vector<T>();
    }
};

如何为上述两个实现定义一个公共(public)接口(interface)类,例如 std::vector<T> foo()是为此接口(interface)定义的吗?忽略函数的实现是相同的。

更新:

我正在编写一个 Container 类,它表示通过 C api 发送给我的数据。

我的容器实例将存储给定类型的数据,例如 Container<int>, Container<std::string> and Container<Foo> .

C api 以一种非常尴尬的方式返回数据,这种情况将来可能会改变。我可以将数据复制到例如 std::list 或 std::vector 中,但由于从 C api 传递了如此多的数据,因此尚不知道这是否可行。

因此,Container 类应该独立于数据的实际存储方式。我使用传递给构造函数的 Getter 和 Setter 类来实现此目的,如下所示:

Container<int>(Getter1<int>(uglyCApiContainer),Setter1<int>(uglyCApiContainer));

因此,如果我放弃处理C api如何存储数据的Getter1和Getter2,我只需要更改Container的创建即可。

但是,我对这个设计有疑问。类型 Foo。

Foo 是一个复杂类型,它本身包含一组容器。目前它看起来像这样:

class Foo
{
public:
        ...
    template <class V>
    Container<V> getMember(std::string& memberName)
};

所以一个给定的 Foo 可以有一组不同类型的容器。我提前知道这些成员的类型,因为它们存储在模型中。 Foo 目前是丑陋的 C api 内存实现的包装器,但我也想为 Foo 分离内存表示,就像我为容器所做的那样。

我不确定如何使 Foo 摆脱其内存实现。我的一个想法是使 getMember 虚拟化,以便引入可能不同的实现,但这对于模板化函数来说是不可能的。

最佳答案

这是使用标记分派(dispatch)和虚拟继承的解决方案:

#include <vector>

template<typename T> struct tag {};

template<typename T> class IFooImpl {
public:
    virtual std::vector<T> getImpl(tag<T>) = 0;
};

class IFoo: public virtual IFooImpl<char>, virtual IFooImpl<int>
{
public:
    template<typename T> std::vector<T> get() {
        return static_cast<IFooImpl<T> *>(this)->getImpl(tag<T>{});
    }
};

template<typename T>
class FooImpl: public virtual IFooImpl<T> {
public:
    std::vector<T> getImpl(tag<T>) { return {}; }
};

class Foo: public IFoo, FooImpl<char>, FooImpl<int> {
};

int main() {
    Foo().get<char>();
}

在涵盖支持的类型(这里是 charint)的地方有一点重复,但是可以通过可变参数模板继承来避免:

#include <vector>

template<typename T> struct tag {};

template<template<typename> class C, typename... Types> class Inherit {};
template<template<typename> class C, typename T, typename... Rest>
class Inherit<C, T, Rest...>: public C<T>, Inherit<C, Rest...> {};

template<typename T> class IFooImplV {
public:
    virtual std::vector<T> getImpl(tag<T>) = 0;
};
template<typename T> class IFooImpl: public virtual IFooImplV<T> {};

template<typename... Types> class IFoo: public Inherit<IFooImpl, Types...> {
public:
    template<typename T> std::vector<T> get() {
        return static_cast<IFooImpl<T> *>(this)->getImpl(tag<T>{});
    }
};

template<typename T> class FooImpl: public IFooImpl<T> {
public:
    std::vector<T> getImpl(tag<T>) { return {}; }
};

template<typename... Types> class FooMulti:
    public IFoo<Types...>, Inherit<FooImpl, Types...> {};
class Foo: public FooMulti<char, int> {};

int main() {
    Foo().get<char>();
}

关于c++ - 具有模板化函数的类的接口(interface)类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12300876/

相关文章:

C++:声明 multimap 迭代器时出错

c++ - 可变参数模板模板参数可以部分特化吗?

templates - 如何获取 D 模板中函数别名的名称?

c++ - 链接到我的主目录中的共享对象文件

c++ - 返回不同类型的变量

c++ - 模板初始化构造函数错误

c++ - 这种语法合法吗?

c++ - 如何在模板元程序中做短路条件?

c++ - reinterpret_cast to void* 不使用函数指针

c++ - Valgrind: fatal error :memcheck.h:没有那个文件或目录