Note that there are some explanatory texts on larger screens.

plurals
  1. POLet the compiler make the final choice which type to use
    primarykey
    data
    text
    <p>This question requires knowledge of C++ template meta-programming as (indirectly) expression templates are involved. I say indirectly because its not directly a question on expression templates, but involves C++ type computations. If you don't know what that is please don't answer this question.</p> <p>To avoid putting out a question without enough background information let me elaborate a bit on the general problem I am trying to solve and then go to the more specific parts.</p> <p>Suppose you have a library that provides <code>Integer</code>s that the user can do calculations with just like with <code>int</code>s. Furthermore it is possible to construct a <code>Integer</code> from an <code>int</code>. Just like:</p> <pre><code>Integer&lt;int&gt; i(2); </code></pre> <p>Internally my <code>Integer</code> class is a class template:</p> <pre><code>template&lt;class T&gt; class Integer { // cut out }; </code></pre> <p>So I can define it on whatever integer type I like.</p> <p>Now without changing the API, I would like to change the library in a way that if <code>Integer</code> was constructed from an <code>int</code> it should be represented internally by a different type, say <code>IntegerLit</code>. The reason for this is that I can speed up some calculation knowing that an instance of <code>Integer</code> was created from an <code>int</code> (can pass it as a <code>int</code> argument to a function instead of as a general object described by a base pointer + separate data. This just as a comment.)</p> <p>It is essential that the <strong>type is different</strong> when constructing from an <code>int</code> because I need the compiler to take up different code paths depending on whether constructed from an <code>int</code> or not. I cannot do this with a runtime data flag. (The reason in short: The compiler generates a function that takes either an <code>int</code> or the above mentioned more general type of object depending on the type.)</p> <p>Having this said I run into a problem: When the uses does something like this:</p> <pre><code>Integer&lt;int&gt; a,b(2); a = b + b; </code></pre> <p>Here <code>a</code> should be the general <code>Integer</code> and <code>b</code> the specialized <code>IntegerLit</code>. However, my problem is how to express this in C++ as the user is free to use the very same type <code>Integer</code> to define her variables.</p> <p>Making the types polymorphic, i.e. deriving <code>IntegerLit</code> from <code>Integer</code> won't work. It looks fine for a moment. However since the user creates instances of <code>Integer</code> (the base class) that won't work because it is the base class the compiler sticks into the expression tree (this is why expression templates are involved in the question). So again no distinction is possible between the two cases. Doing a RTTI check a la dynamic cast is really not what I want on that point.</p> <p>More promising seems to be adding a literal template parameter <code>bool lit</code> to the type saying if it was constructed from an <code>int</code> or not. The point is to not specify the conversion rule for one not literal one but do specify it for the other case.</p> <p>However, I can't get that to work. The following code only compiles (GCC 4.7 C++11) if not constructing from an <code>int</code>. Otherwise it fails because the Integer is not specified with <code>true</code> as the value for <code>lit</code>. So the compiler searches the default implementation which doesn't have the conversion rule. It is not an option changing the API and requiring to write <code>Integer&lt;int,true&gt;</code> when constructing from an <code>int</code>.</p> <pre><code>template&lt;class T,bool lit=false&gt; class Integer { public: Integer() { std::cout &lt;&lt; __PRETTY_FUNCTION__ &lt;&lt; "\n"; } T F; }; template&lt;&gt; template&lt;class T&gt; class Integer&lt;T,true&gt; { public: Integer(int i) { std::cout &lt;&lt; __PRETTY_FUNCTION__ &lt;&lt; "\n"; } T F; }; </code></pre> <p>I am starting wondering if something like this is possible with C++.</p> <p>Is there maybe a new feature of C++11 that can help here?</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.
    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