c++ template policy

template<typename... Policies>
class PolicyAndVariadic: public Policies...{};

class PolicyOne
{
public:
    void executePolicyOne()
    {
        std::cout << "execute PolicyOne" << std::endl;    
    }
};

class PolicyTwo
{
public:
    void executePolicyTwo()
    {
        std::cout << "execute PolicyTwo" << std::endl;
    }
};

typedef PolicyAndVariadic<PolicyOne, PolicyTwo> PolicyOneAndPolicyTwo;

PolicyOneAndPolicyTwo linstance;

linstance.executePolicyOne();

linstance.executePolicyTwo();

3.88
8
Awgiedawgie 440220 points

                                    template&lt;typename... Policies&gt;
class PolicyAndVaradic: public Policies...
{
public:
    template&lt;typename... Args&gt;
    PolicyAndVaradic(const Args... Arg)
        : Policies(Arg)...{}
};

class PolicyOne
{
    std::string mText;

public:
    PolicyOne(const char* aText):mText(aText){}
    
    void executePolicyOne()
    {
        std::cout &lt;&lt; mText &lt;&lt; std::endl;
    }
};

class PolicyTwo
{
    std::string mText;
public:

    PolicyTwo(const char* aText):mText(aText){}

    void executePolicyTwo()
    {
        std::cout &lt;&lt; mText &lt;&lt; std::endl;
    }
};

PolicyOneAndPolicyTwo linstance(&quot;PolicyOne&quot;, &quot;PolicyTwo&quot;);

linstance.executePolicyOne();

linstance.executePolicyTwo();

3.88 (8 Votes)
0
4
6
A-312 69370 points

                                    #include &lt;iostream&gt;
#include &lt;string&gt;

template &lt;typename OutputPolicy, typename LanguagePolicy&gt;
class HelloWorld : private OutputPolicy, private LanguagePolicy {
 public:
  // Behavior method.
  void Run() const {
    // Two policy methods.
    Print(Message());
  }

 private:
  using LanguagePolicy::Message;
  using OutputPolicy::Print;
};

class OutputPolicyWriteToCout {
 protected:
  template &lt;typename MessageType&gt;
  void Print(MessageType&amp;&amp; message) const {
    std::cout &lt;&lt; message &lt;&lt; std::endl;
  }
};

class LanguagePolicyEnglish {
 protected:
  std::string Message() const { return &quot;Hello, World!&quot;; }
};

class LanguagePolicyGerman {
 protected:
  std::string Message() const { return &quot;Hallo Welt!&quot;; }
};

int main() {
  // Example 1
  using HelloWorldEnglish = HelloWorld&lt;OutputPolicyWriteToCout, LanguagePolicyEnglish&gt;;

  HelloWorldEnglish hello_world;
  hello_world.Run();  // Prints &quot;Hello, World!&quot;.

  // Example 2
  // Does the same, but uses another language policy.
  using HelloWorldGerman = HelloWorld&lt;OutputPolicyWriteToCout, LanguagePolicyGerman&gt;;

  HelloWorldGerman hello_world2;
  hello_world2.Run();  // Prints &quot;Hallo Welt!&quot;.
}

4 (6 Votes)
0
Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source