Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can have the policy classes contain static functions so ideally you would want FileOutputter to look like:</p> <pre><code> template&lt;std::string fileName&gt; struct FileOutputter { static void out(const char* msg) const { std::ofstream out(fileName); out &lt;&lt; msg; } }; </code></pre> <p>You would create an instance of LoggerImpl like this</p> <pre><code>LoggerImpl&lt;NoFilter, SimpleFormatter, FileOutputter&lt;"log.txt"&gt; &gt; FileLogger; </code></pre> <p>This would mean your LoggerImpl doesn't need to store a copy of the policy classes it just need to call their static functions. Unfortunately this won't work because you can't have strings as template arguments but you can build a table of strings and pass the index of the filename in your string table. So again you would ideally want this to look like:</p> <pre><code>//This class should be a singleton class StringManager { std::vector&lt;std::string&gt; m_string; public: void addString(const std::string &amp;str); const std::string &amp;getString(int index); int getIndexOf(const std::string &amp;str); }; </code></pre> <p>Then your FileLogger would get an int as a template parameter and it would be the index of the string in the StringManager. This also wouldn't quite work because you need the index available at compile time and StringManager would be initialized at run time. So you would have to build the string table manually and manually write in the index of your string. so your code would look like (after you make StringManager a singleton:</p> <pre><code>StringManager::instance()-&gt;addString("a"); StringManager::instance()-&gt;addString("b"); StringManager::instance()-&gt;addString("c"); StringManager::instance()-&gt;addString("d"); StringManager::instance()-&gt;addString("log.txt"); LoggerImpl&lt;NoFilter, SimpleFormatter, FileOutputter&lt;4&gt; &gt; FileLogger; </code></pre> <p>You would have to make sure that StringManager is fully initialized before the first instance of FileLogger is created. It's a little ugly but using templates with strings is a little ugly. You could also do something like:</p> <pre><code>template &lt;class FilenameHolder&gt; struct FileOutputter { static void out(const char* msg) const { std::ofstream out(FilenameHolder::fileName()); out &lt;&lt; msg; } }; class MyFileNameHolder { static std::string fileName() {return "log.txt";} }; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload