Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is one of cases much more tricky than one can initially expect.</p> <p>One of important and useful features D has over C++ is that every single type (including all user types) has some initial non-garbage value that can be evaluated at compile-time. It is used as <code>T.init</code> and has two important use cases:</p> <ol> <li><p>Template constraints can use T.init value to check if certain operations can be done on given type (quoting Kenji Hara's snippet):</p> <pre><code>template isSomething(T) { enum isSomething = is(typeof({ //T t1; // not good if T is nested struct, or has @disable this() //T t2 = void; auto x = t2; // not good if T is non-mutable type T t = T.init; // avoid default construct check ...use t... })); } </code></pre></li> <li><p>Your variables are always initialized properly unless you explicitly use <code>int i = void</code> syntax. No garbage possible.</p></li> </ol> <p>Given that, difficult question arises. Should we guarantee that T() and T.init are the same (as many programmers coming from C++ will expect) or allow default construction that may easily destroy that guarantee. As far as I know, decision was made that first approach is safer, despite being surprising.</p> <p>However, discussions keep popping with various improvements proposed (for example, allowing CTFE-able default constructor). One <a href="http://forum.dlang.org/post/mailman.1362.1368826464.4724.digitalmars-d@puremagic.com" rel="noreferrer">such thread</a> has appeared very recently.</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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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