Note that there are some explanatory texts on larger screens.

plurals
  1. POtemplate function specialization problem
    primarykey
    data
    text
    <p>I am using templates to implement a range checked conversion from int to enum. It looks like this:</p> <pre><code>template&lt;typename E&gt; E enum_cast(const int &amp;source); </code></pre> <p>The template function placed more or less in the root-directoy of the project. When defining a new enum that is foreseen to be assigned values from a config file like this:</p> <pre><code>enum ConfigEnum { ConfigEnumOption1 = 'A' , ConfigEnumOption2 = 'B' , ConfigEnumInvalid }; ConfigEnum option = XmlNode.iAttribute("option"); </code></pre> <p>I define a template specialization for this particular enum type in a .cpp-file for the module this enum is used in.</p> <pre><code>template&lt;&gt; ConfigEnum enum_cast(const int &amp;source) { switch(source) { case ConfigEnumOption1 : return ConfigEnumOption1; case ConfigEnumOption2 : return ConfigEnumOption2; default return ConfigEnumInvalid; } </code></pre> <p>Now the assignment of an int to the enum becomes:</p> <pre><code>ConfigEnum option = enum_cast&lt;ConfigEnum&gt;(XmlNode.iAttribute("option")); </code></pre> <p>which makes sure that the enum is allways in valid range. Note that I have not allways control over these enums so this seems to a be reasonable and easily configureable solution. </p> <p>Anyway, this works all very well (although I am not shure all code given here is correct because I just recall it from memory right now)</p> <p>The problem stems from the fact that it might be desireable to use this "enum_cast" construct thorughout the code base whenever an in is assigned to a enum. After all this can be enforced by a simple search-and-replace operation. Of course I do not want to define these specializations for all and every enum around but only for those that need the range check at the moment. I would prefer to add template specializations for the enum types when the need arises and use the assignment operator when no specialization is defined.</p> <p>Thus:</p> <pre><code>InternalEnum internal = enum_cast&lt;InternalEnum&gt;(internal_integer); </code></pre> <p>would effecively call internal = internal_integer. I figure that I need to tell the compiler to use a certain "default" implementation for all enum types that do not have a specialization.</p> <p>My first bet was giving the original template function an implementation like this:</p> <pre><code>template&lt;typename E&gt; E enum_cast(const int &amp;source) { E copy = source; return copy; }; </code></pre> <p>Unfortunately now this is allways called instead of the specialiazations given in the .cpp-files deeper into the project directory-tree.</p> <p>Any thoughts? </p> <p>Thanks in advance Arne</p>
    singulars
    1. This table or related slice is empty.
    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.
    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