Applying policy based design question

Posted by Arthur on Stack Overflow See other posts from Stack Overflow or by Arthur
Published on 2010-04-04T20:14:13Z Indexed on 2010/04/04 20:23 UTC
Read the original article Hit count: 395

Filed under:
|

I've not read the Modern C++ Design book but have found the idea of behavior injection through templates interesting. I am now trying to apply it myself.

I have a class that has a logger that I thought could be injected as a policy. The logger has a log() method which takes an std::string or std::wstring depending on its policy:

// basic_logger.hpp
template<class String>
class basic_logger
{
public:
    typedef String string_type;

    void log(const string_type & s) { ... }
};
typedef basic_logger<std::string> logger;
typedef basic_logger<std::wstring> wlogger;

// reader.hpp
template<class Logger = logger>
class reader
{
public:
    typedef Logger logger_type;

    void read()
    {
        _logger.log("Reading...");
    }

private:
    logger_type _logger;
};

Now the questing is, should the reader take a Logger as an argument, like above, or should it take a String and then instantiate a basic_logger as an instance variable? Like so:

template<class String>
class reader
{
public:
    typedef String string_type;
    typedef basic_logger<string_type> logger_type;

    // ...

private:
    logger_type _logger;
};

What is the right way to go?

© Stack Overflow or respective owner

Related posts about c++

Related posts about policy-injection