Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ template specialization to provide/add different member functions
    primarykey
    data
    text
    <p>I'm experimenting a little with type traits and template specialization. For example:</p> <pre><code>enum TestEnum { VALUE0 = 0, VALUE1 = 1, VALUE2 = 2 //... And so on... }; template&lt;int Value&gt; class cTestClass; //specializations template&lt;&gt; class cTestClass&lt;VALUE0&gt; { static int GetVal() { return VALUE0; } }; template&lt;&gt; class cTestClass&lt;VALUE1&gt; { static int GetVal() { return VALUE1; } }; template&lt;&gt; class cTestClass&lt;VALUE2&gt; { static int GetVal() { return VALUE2; } }; typedef cTestClass&lt;VALUE0&gt; cClassVal0; //(1) typedef cTestClass&lt;VALUE1&gt; cClassVal1; //(2) typedef cTestClass&lt;VALUE2&gt; cClassVal2; //(3) //Later in the code template&lt;int Value&gt; cTestClass&lt;Value&gt;* Create() { return new cTestClass&lt;Value&gt;(/*..*/); } //And using the upper function cClassVal2* pTestClass = Create&lt;VALUE2&gt;(); </code></pre> <p>This works absolutely fine. The goal is to provide a way for users of an interface not to deal with templates and template parameters (Ok, besides using <em>Create()</em>). That is the reason for the typedefs at the bottom of the code.</p> <p>No here is what I want to achieve next: cTestClass should have one member variable which is accessible via member functions. Under certain circumstances I either want the user being able to modify the member variable or not to modify the member variable. So basically this means two member functions, one that returns the member as const reference (not modifyable) or non const (modifyable). Problem is, if both those member functions are provided, the user always has the chance to modify the member variable. So I want to choose the appropriate member function during compile time and just provide one correct member function for the user.</p> <p>The only solution I came up with looks basically like this:</p> <pre><code>template&lt;int Value, bool ConstAccess&gt; class cTestClass; //specializations template&lt;&gt; class cTestClass&lt;VALUE0, true&gt; { const std::string&amp; get() const { return m_strName; } static int GetVal() { return VALUE0; } private: std::string m_strName; }; template&lt;&gt; class cTestClass&lt;VALUE0, false&gt; { std::string&amp; get() { return m_strName; } static int GetVal() { return VALUE0; } private: std::string m_strName; }; //For every upcoming cTestClass two specializations typedef cTestClass&lt;VALUE0, true&gt; cConstObjectVal0; typedef cTestClass&lt;VALUE0, false&gt; cObjectVal0; //And for the rest... </code></pre> <p>Besides the fact that this really seams to be unnecessary code duplication I always have to specify two typedefs for every possible VALUE. But the user should not be confronted with that choice, he should always be able to just use the types as given above (see 1, 2, 3).</p> <p>Is this even possible?</p> <p>I googled a bit and came up with the following site: <a href="http://accu.org/index.php/journals/442" rel="nofollow noreferrer">ACCU:: An introduction to C++ traits</a>. This explanation seems to a matching blueprint, but I really can't put the pieces together. Another possibility I found is described here: <a href="https://stackoverflow.com/questions/1699015/c-template-specialization-to-provide-extra-member-function">C++ Template specialization to provide extra member function?</a> Just adding one function would be possible, but this still doesn't solve my typedef problem.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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