Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ and the const reference to temporary binding issue (Implementing the D language pass by value and by reference rules in C++0X)
    primarykey
    data
    text
    <p>I was wondering to what extend it's possible to mimic the D language rules for pass by value and pass by reference rules in C++. For background please see the following two references (mostly Alexandrescu):</p> <p><a href="http://bartoszmilewski.wordpress.com/category/d-programming-language/page/2/" rel="nofollow">http://bartoszmilewski.wordpress.com/category/d-programming-language/page/2/</a></p> <p>and</p> <p><a href="http://groups.google.com/group/comp.std.c++/msg/303e3bf2407a7609" rel="nofollow">http://groups.google.com/group/comp.std.c++/msg/303e3bf2407a7609</a>?</p> <p>One of the key differences is that in D const references does not bind (as non const ones) to temporaries.</p> <p>However, I am not aware of any way to define a generic class X in such a way that the following code will fail to compile:</p> <pre><code>void f(const X&amp; x) {...} f( X() ); //Cannot disable binding of const ref to X </code></pre> <p>One possibility could be to make f a template function, check rvalue/lvalue-ness of the argument passed (possible in C++0X) and use disable_if but this clutters the code too much and does not scale well.</p> <p>Another possibility could be to introduce a template class like</p> <pre><code>template&lt;class T&gt; Ref&lt;T&gt; : public T {...} //D-style ref, does not bind to temporaries! </code></pre> <p>and then use </p> <pre><code>void f(Ref&lt;const X&gt; x) {...} //Does not look bad.... f( X() ); //Compile error here is doable, I checked a similar example already... </code></pre> <p>However, this way I loose the ability to write template functions taking Ref as the following will not compile...</p> <pre><code>template&lt;class T&gt; void ft(Ref&lt;const T&gt; x) {...} ft( X() ); //Template deduction error </code></pre> <p>What are your thoughts? Any suggestion/comment/help is appreciated!</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.
 

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