递归继承,用于重载一组模板参数的方法 [英] recursive inheritance for overloading a method for a set of template arguments

查看:258
本文介绍了递归继承,用于重载一组模板参数的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要找到一种方法,以给定一组模板参数来递归地构建一个类,以便该类从其自身继承,并为模板参数列表中的当前第一个模板参数构建一个方法f,然后从通过传递列表的其余部分本身.

I need to find a way to recursively build a class given a set of template arguments so that the class inherits from itself and build a method f for the current first template argument in the list of template arguments and then inherits from itself by passing on the rest of the list.

因此,基本上,我想为类C实现以下接口:

So, basically I want to achieve the following interface for a class C:

C<T1, T2, T3> c;

c现在具有方法C::f(T1)C::f(T2)C::f(T3)

到目前为止,我的方法是这样的:

My approach so far was like this:

// primary template
template <class H, class...>
class C {};

// base case where class... is empty
template <class H, class...>
class C<H>
{
public:
    void f(const H& h){
        // std::cout << typeid(h).name() << "\n";
    }
};

// recursive case where T is nonempty
template <class H, class... T>
class C : public C<T...>
{
public:
    void f(const H& h){
        // std::cout << typeid(h).name() << "\n";
    }
};

据我所知,这实际上并不能编译

This does not actually compile, as I get

错误:重新定义"C"类C:公共C

error: redefinition of 'C' class C : public C

我的方法基本上可行吗,只是一个语义和/或句法上无效的代码问题,还是该方法原则上不起作用?

Is my approach basically possible and just a matter of semantically and or syntactically invalid code or does this approach not work in principle?

推荐答案

对于初学者来说,类不能从其自身继承.

For starters, a class cannot inherit from itself.

第二,您显然想完成的一切就是让每个模板参数生成一个将该类作为参数的类方法.

Secondly, all that you apparently are trying to accomplish is to have each template parameter generate a class method that takes that class as a parameter.

在这种情况下,类似的事情应该起作用.

In which case, something like this should work.

template<typename ...> class C;

template<>
class C<> {};

template<typename T, typename ...Args>
class C<T, Args...> : public C<Args...> {

public:

    void f (const T &)
    {
       // Whatever...
    }
};

请注意,这不是从其自身继承的类.这是一个从另一个模板实例继承的模板实例.每个模板实例都是一个唯一的类.

Note that this is not a class inheriting from itself. It's a template instance that inherits from another template instance. Each template instance is a unique class.

请注意,您在此处仅对一个有关类方法的定义进行了定义,而不是像您尝试的那样对两个方法进行定义.这是一个小改进.

Note that you have a single definition of the class method in question here, and not two, as you were trying to do. This is a slight improvement.

如果可能的话,考虑到您的其他班级要求,另一种改进是考虑以这种方式重新布置班级层次结构:

Another improvement would be to consider rearranging the class hierarchy in this manner, if it's possible to do this taking into consideration your other class requirements:

template<typename T> class F {
public:

    void f (const T &)
    {
    }
};


template<typename ...> class C;

template<>
class C<> {};

template<typename T, typename ...Args>
class C<T, Args...> : public C<Args...> , public F<T> {

};

使用这种方法,无论使用C<int, float>还是C<int, char *>,类方法始终将声明为F<int>的方法.这样会稍微减少代码浮点数,因为例如包含int的任何C实例都将生成一个类方法实例,而不是像C<int, float>::f(const int &)C<int, char *>::f(const int &)这样的两个单独方法, ,否则,请完全相同.

With this approach, whether you use C<int, float>, or C<int, char *>, the class method will always be declared to be a method of F<int>. This cuts down slightly on the resulting code float, since any instance of C that includes int, for example, will generate a single class method instance, instead of two separate methods like C<int, float>::f(const int &) and C<int, char *>::f(const int &), which would, otherwise, be completely identical.

这篇关于递归继承,用于重载一组模板参数的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆