使用变量类型返回值的C ++基于策略的设计 [英] C++ Policy Based Design With Variable Type Return Value

查看:109
本文介绍了使用变量类型返回值的C ++基于策略的设计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用基于策略的设计,使主机类的返回类型根据我使用的策略而改变。下面是一个例子:

I want to used Policy Based Design to have the return type of a host class change based on the policies I'm using. Here is an example:

class IntPolicy {
public:
    int doIntPolicy(double anInput) {
        return static_cast<int>(anInput);
    }
};

class DoublePolicy {
public:
    double doDoublePolicy(int anInput) {
        return static_cast<double>(anInput);
    }
};

template<typename IntPolicyType, typename DoublePolicyType>
class SimpleHost {

private:
    IntPolicyType _intPolicy;
    DoublePolicyType _doublePolicy;

public:

    template<typename InputType>
    auto doHost(InputType input) -> decltype(_doublePolicy.doDoublePolicy(_intPolicy.doIntPolicy(input))) {
        auto aVar =  _intPolicy.doIntPolicy(input);
        return _doublePolicy.doDoublePolicy(aVar);
    }


};

这里是我如何使用主机类和策略:

Here is how I'd use the host class and policies:

typedef SimpleHost<IntPolicy, DoublePolicy> Host;
Host host;
auto theOutput = host.doHost(5);

当编译和工作时,注意我必须将doHost方法的主体放在decltype函数,以便编译器可以推导出尾随返回。如果doHost函数的主体是大的,那么这将看起来令人难以置信的丑陋。

While this compiles and works, notice that I must essentially put the body of the doHost method inside the decltype function so that the compiler can deduce the trailing return. If the body of the doHost function was large then this would look incredibly ugly. Is there any way I can avoid this?

推荐答案

1.-扩展模板

#include <stdio.h>
#include <string>
#include <iostream>

class IntPolicy {
public:
    int doIntPolicy(double anInput=NULL) {
        if(!NULL)
        {
            return static_cast<int>(anInput);
        }
        else
        {
            return -1;
        }
    }
};

class DoublePolicy {
public:
    double doDoublePolicy(int anInput=NULL) {
        if(!NULL)
        {
            return static_cast<double>(anInput);
        }
        else
        {
            return -1;
        }
    }

};



template<typename IntPolicyType, typename DoublePolicyType,
    class __Type >
class SimpleHost {

private:
    IntPolicyType _intPolicy;
    DoublePolicyType _doublePolicy;

public:

    template<typename InputType>
    auto doHost(InputType input) -> __Type {
        auto aVar =  _intPolicy.doIntPolicy(input);
        return _doublePolicy.doDoublePolicy(aVar);
    }



};

int main()
{
    IntPolicy foo;
    DoublePolicy bar;
    typedef SimpleHost<IntPolicy, DoublePolicy, 
        decltype(bar.doDoublePolicy(foo.doIntPolicy()))> Host;
    Host host;
    auto theOutput = host.doHost(5);

    return 0;
}

2.-最简单的方法是重命名impl使用外部库您可以考虑包装它为此目的

2.- The simplest way would be to rename the impl, if you are using an external library you could consider Wrapping it for this purpose

class IntPolicy {
public:
    int doPolicy(double anInput) {
        return static_cast<int>(anInput);
    }
};

class DoublePolicy {
public:
    double doPolicy(int anInput) {
        return static_cast<double>(anInput);
    }

};



template<typename IntPolicyType, typename DoublePolicyType>
class SimpleHost {

private:
    IntPolicyType _intPolicy;
    DoublePolicyType _doublePolicy;

public:

    template<typename InputType>
    auto doHost(InputType input) -> decltype(_doublePolicy.doPolicy(_intPolicy.doPolicy(input))) 
    {
        auto aVar =  IntPolicyType.doPolicy(input);
        return DoublePolicyType.doPolicy(aVar);
    }

但是要真正帮助你,我们需要你想调用的特定函数,我的意思是我觉得这是一个混乱已经。

But to really help you, we need the specific functions you are trying to call, I mean I feel like this is a mess already.

这篇关于使用变量类型返回值的C ++基于策略的设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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