Answers for "c++ template policy"

C++
0

c++ template policy

template<typename... Policies>
class PolicyAndVaradic: public Policies...
{
public:
    template<typename... Args>
    PolicyAndVaradic(const Args... Arg)
        : Policies(Arg)...{}
};

class PolicyOne
{
    std::string mText;

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

class PolicyTwo
{
    std::string mText;
public:

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

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

PolicyOneAndPolicyTwo linstance("PolicyOne", "PolicyTwo");

linstance.executePolicyOne();

linstance.executePolicyTwo();
Posted by: Guest on July-24-2021
0

c++ template policy

#include <iostream>
#include <string>

template <typename OutputPolicy, typename LanguagePolicy>
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 <typename MessageType>
  void Print(MessageType&& message) const {
    std::cout << message << std::endl;
  }
};

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

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

int main() {
  // Example 1
  using HelloWorldEnglish = HelloWorld<OutputPolicyWriteToCout, LanguagePolicyEnglish>;

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

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

  HelloWorldGerman hello_world2;
  hello_world2.Run();  // Prints "Hallo Welt!".
}
Posted by: Guest on July-24-2021
0

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();
Posted by: Guest on July-24-2021

Browse Popular Code Answers by Language