C ++ Policy Based Design with Variable Type Return Value

I want to use Policy Based Design to have a return class type of host class change based on the policies used. 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's how I would use the class and host policies:

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

While this is compiling and working, note that I should essentially put the body of the doHost method inside the decltype function so that the compiler can output the final return. If the body of the doHost function was large, then it would look incredibly ugly. Is there any way to avoid this?

0
source share
2 answers

If you don’t like how it looks, probably try to determine?

 #define RECURSE_POLICY(E) policy2.doPolicy(policy1.doPolicy(E)) template<typename InputType> auto doHost(InputType input) -> decltype(RECURSE_POLICY(input)) { return RECURSE_POLICY(input); } 

Having said that, something doesn't look right, that you should call doPolicy twice. Perhaps you can reorganize it.

0
source

1.- Template extension

 #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.- The easiest way is to rename impl if you are using an external library that you could consider. Wrap 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 in order to really help you, we need specific functions that you are trying to call, I mean, I feel that this is already a mess.

0
source

All Articles