Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating a new primitive type
    primarykey
    data
    text
    <p>Is there a way to create a new type that is like one of the basic types (eg char), and can be implcitly converted between, but will resolve diffrently in templates, such that for example, the following code works?</p> <pre><code>typedef char utf8; template&lt;typename T&gt;void f(T c); template&lt;&gt; void f&lt;char&gt;(char c) { std::cout &lt;&lt; "ascii " &lt;&lt; c &lt;&lt; std::endl; } template&lt;&gt; void f&lt;utf8&gt;(utf8 c)//error C2766: explicit specialization; 'void f&lt;char&gt;(char)' has already been defined { std::cout &lt;&lt; "utf8 " &lt;&lt; c &lt;&lt; std::endl; } int main() { char c1 = 'x'; utf8 c2 = 'g'; f(c1); f(c2); } </code></pre> <p>I'm thinking that it may be possible with a class containing a single data member, if so what is the cleanest way to do it, and will compilers be able to optimise it as if it was a primitive.</p> <p>EDIT: I tried BOOST_STRONG_TYPEDEF, and that seems to work for basic things, but how can I then go onto create a std::basic_string from the new type?</p> <pre><code>BOOST_STRONG_TYPEDEF(char,utf8); //first try BOOST_STRONG_TYPEDEF(std::string,utf8_string); //second try typedef std::basic_string&lt;utf8, std::char_traits&lt;utf8&gt;,std::allocator&lt;utf8&gt; &gt; uft8_string; </code></pre> <p>The first one doesn't really work because the resulting type still expects char for all its methods, and the second one doesn't seem to like the presence of constructors and assignment operators :(</p> <p>I haven't tried the other way to create the new char type yet, will it be able to get around this if I cant with BOOST_STRONG_TYPEDEF?</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