使用公共基(C ++)乘以从函数对象继承 [英] Multiply inheriting from function objects with a common base (C++)

查看:189
本文介绍了使用公共基(C ++)乘以从函数对象继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些没有成员变量的函数对象。函数对象本质上非常简单。它们都继承自 unary_function<> binary_function<> 。例如,一对函数对象可能是这样的:

I have a few function objects that have no member variables. The function objects are very simple in nature. They all inherit from unary_function<> or binary_function<>. For example, a couple of the function objects may be something like this:

struct key_to_hash_method_1 : public binary_function<int, int, int>
{
  int operator() (int a, int b) const { /* do something */ }
};

template <typename key_to_hash_method>
struct hash_shrink_method_1 : public binary_function<int, int, int>, public key_to_hash_method
{
  int operator() (int a, int b) const { /* do something while utilizing key_to_hash_method */ }
};

/* and more variations of these function objects */

类通过将它们作为策略的模板参数来使用这些函数对象。然后,模板类继承它们:

A template class uses these function objects by taking them as template parameters as policies. The template class then inherits from them:

template <typename hash_method>
class foo : public hash_method 
{
public:
/* do something while using hash_method as well as using the information provided by binary_function<> to selective compile different functions*/
};

当然,为了保持示例简单,上述内容可能没有太大意义作为有用的。

Of course, in the interest of keeping the example simple, the above may not make much sense as far as usefulness goes.

为什么要继承而不是使用组合?只是为了避免空类占用空间。节省的空间是否是微小的不是问题的要点。

Why am I inheriting instead of using composition? Simply to avoid the empty classes from taking up space. Whether the space saved is minuscule or not is not the point of the question.

从上面的代码中可以看出, binary_function< int,int,int> 这导致了警告(在VC ++ 2008中):

As you can see from the above code, binary_function<int, int, int> will be inherited twice, which gives rise to the warning (in VC++ 2008):

Warning 1   warning C4584: 'hash_shrink_method_1<key_to_hash_method>' : base-class 'std::binary_function<_Arg1,_Arg2,_Result>' is already a base-class of 'key_to_hash_method_1'    c:\visual studio 2008\projects\defaulttemplatearguments\main.cpp    12

现在一般来说,在多重继承中,这是通过虚拟继承来解决的;我想在这种情况下避免。在这种情况下我可以做什么删除警告?

Now generally, in multiple inheritance, this is solved by virtual inheritance; which I want to avoid in this case. What can I do in this situation to remove the warning?

我的直接解决方案是不继承 binary_function<> ,因为我假设 key_to_hash_method 将是 binary_function 。这个解决方案感觉有点像一个程序员谁没有访问包括警卫或 pragma一次语句。是的,他可以避免包括标题两次,但他宁愿编译器找出他。我想在这种情况下相同。

My immediate solution is to not inherit from binary_function<> since I am assuming that key_to_hash_method will be a binary_function. This solution feels a bit like a programmer who does not have access to include guards or pragma once statement. Yes, he can avoid including the header twice, but he'd rather the compiler figure it out for him. I would like the same in this case.

示例代码

Example code, if you want to try it out:

#include <functional>

using namespace std;

struct key_to_hash_method_1 : public binary_function<int, int, int>
{
  int operator() (int a, int b) const { return a + b; }
};

template <typename key_to_hash_method>
struct hash_shrink_method_1 : public binary_function<int, int, int>, public key_to_hash_method
{
  int operator() (int a, int b) const { return key_to_hash_method::operator()(1, 2) * 5; }
};

template <typename hash_method>
class foo : public hash_method 
{
public:
  int test() 
  { 
    /* in actual code, this function selectively calls other functions 
       depending on whether hash_method is unary or binary */ 
    return hash_method::operator()(5, 6); 
  }
};

int main()
{
  foo<hash_shrink_method_1<key_to_hash_method_1> > f;
  printf("%i\n", f.test());
}


推荐答案

您的 hash_shrink_method_1 不需要直接继承 binary_function ,因为你假设它的参数类 key_to_hash_method 已经这样做。如果你想确定,可以添加一个静态断言( std :: is_base_of );虽然如果你已经有C ++ 11,你可以取消过时的 binary_function

Your hash_shrink_method_1 doesn't need to inherit from binary_function directly, since you assume that its parameter class key_to_hash_method already does so. You can add a static assertion (std::is_base_of) if you want to be sure; though if you already have C++11, you can do away with the obsolete binary_function anyway.

这篇关于使用公共基(C ++)乘以从函数对象继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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