Last update September 14, 2010

Doc Comments /
CPPto D



Converting C++ to D

Comments

Structs as Properties

structs do *not* work as properties, since they are returned by value

Array Sorting

If in D we have an array of a given class, it is possible to sort it according to an overloaded opCmp(Object) function. See web pages: http://www.digitalmars.com/d/archives/digitalmars/D/20918.html

See Code:

  import std.stdio;
  import std.random;

  class Cl {
      int i;

      int opCmp( Object B ) {
  	writef(".");
  	return i - (cast(Cl)B).i;
      }
  };

  int main() {
    int[5] array_int;
    Cl[5] array_Cl;

    writef("Array of int:\n");
    foreach (ref int i; array_int) {
	i = cast(int)rand();
	writefln(i);
    }
    writefln();

    writef("Array of classes:\n");
    foreach (ref Cl A; array_Cl) {
	A = new Cl;
	A.i = cast(int)rand();
	writefln(A.i);
    }
    writef("\n\n");

    array_int.sort;
    array_Cl.sort;

    writef("\n\nArray of int:\n");
    foreach (int i; array_int) {
	writefln(i);
    }
    writefln();

    writef("Array of classes:\n");
    foreach (Cl A; array_Cl) {
	writefln(A.i);
    }
    writef("\n\n");

    return 0;
  }

Meta Templates

This section considerably overstates the problems with using templates in C++, though D is significantly better.

For example, here is a C++ solution to the integer-parameterized-with-number-of-bits problem which (1) handles non-exact matches, (2) gives clear error messages when unable to provide a suitable type, (3) DOES use relational operators, (4) is reasonably readable and maintainable, and (5) isn't a spectacularly clever technique and doesn't work the compiler hard at all.

  template<bool p0, bool p8, bool p16, bool p32, bool p64>  struct PICK_  {  };

template<> struct PICK_<true, false, false, false, false> { typedef __int8 INT; typedef unsigned __int8 UINT; };

template<> struct PICK_<true, true, false, false, false> { typedef __int16 INT; typedef unsigned __int16 UINT; };

template<> struct PICK_<true, true, true, false, false> { typedef __int32 INT; typedef unsigned __int32 UINT; };

template<> struct PICK_<true, true, true, true, false> { typedef __int64 INT; typedef unsigned __int64 UINT; };

template<int W> struct PICK : public PICK_< (W>0), (W>8), (W>16), (W>32), (W>64) > { };

I wouldn't use this in practice without a very good reason since it doesn't solve the whole problem. You still need to determine the compiler-specific type names, those above being a non-portable compiler-specific extension, and there is rarely a need to go beyond having a bunch of typedefs under conditional compilation. Also, most people would want a syntactic sugar macro. Even so, the method is not so bad if it is needed.

D is better, but when the difference is overstated like this it can create a credibility problem. I end up thinking of those adverts that say the latest product is n percent better, but the fine print says 'compared with the cheapest garbage on the market 50 years ago, applied by a particularly stupid chimpanzee'.

Properties

The example given for using properties in D appears to be broken/out of date! The class reference 'a' would be null as written and needs to be assigned via a = new Abc() before use. Also my understanding is that properties are now supposed to be defined using the @property attribute, although I was surprised that the example compiled and ran after fixing the instantation of 'a' only.

Amendments

You may add a link to this page on the DocumentationAmendments page to draw extra attention to your suggestion.

Links

See the corresponding page in the D Specification.
FrontPage | News | TestPage | MessageBoard | Search | Contributors | Folders | Index | Help | Preferences | Edit

Edit text of this page (date of last change: September 14, 2010 8:13 (diff))