Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Transition from C++ std / Boost to Tango

This page is intended to help people to move from well known C++ libraries to Tango.

More information can be found here.

General C++/C vs D differences

Preprocessor statements

Inclusion guards like those below can be omitted completely.

#ifndef my_class #define my_class 
.. 
..
#endif

#define statements can be replaced by

const uint foo; 

or enums (often recommended to reduce size of executables).

For other #ifndef constructs, static if or version can be used.

Exceptions

Exceptions can be thrown by writing "throw new Exception("My Message");". To get the message from the exception object, use the .toString() member function. A General advice is not to make use of Exceptions for your regular program flow.

Note: The compiler includes array bounds checking and other simple checkings into the binary. These are removed when you compile your code using the "-release" flag. Instead of an out of bounds exception your application will just crash.

Standard C headers

time.h

For manipulating time, use the various modules in the tango.time package.

STL

std::cout

For simple console output you have different choices, e.g.:

Use

Stdout("Hello World!");

from tango.io.Console for text only output.

Use

Stdout.formatln("Hello {}", "World");

from tango.io.Stdout for formatted output

std::string

D provides in a built-in string-like datatype of char[]. It is a struct that contains a char pointer (not \0 terminated) and has a length field. Tango provides tango.text.Util containing advanced string manipulation, although it is not needed for basic string operations such as slicing.

std::string::substr

The equivalent to substr is the slice operation provided by the D language core.

std::string::size / std::string::empty

Every array has a length property that stores the length of the array. It is also the fastest way to determine if an array is empty.

std::string::npos

std::string::npos defines a non existing position (typically in an array).

Tango algorithms do return the size of the array instead, which is also an invalid position. To get this position value for you can access the language buildin .length property of an array.

std::find

tango.core.Array/tango.text.Util has different find*/locate* functions that do the same job and can do even more advanced operations.

Boost

boost::bind

boost::regex

tango.text.Regex

Comments
Author Message

Posted: 09/06/07 18:24:50 -- Modified: 09/12/07 04:10:12 by
baxissimo -- Modified 2 Times

How about some suggestions for how to replace STL's containers with Tango's collections (my guesses below)?

  • std::vector: -- D's built-in arrays or ArraySeq?
  • std::hash_map: -- D's built-in assoc arrays or HashMap?
  • std::list -- LinkSeq?
  • std::map -- TreeMap?
  • std::set -- I guess HashSet is the closest in Tango, but the C++ set guarantees sorted order which can be useful. If Tango had it, TreeSet? would be most like std::set. TreeMap?!(T,bool) would also be a similar type, just ignoring the bool part.
  • std::multiset -- TreeBag?
  • std::hash_set -- HashSet
  • std::multimap -- anything for this? I guess you could sort of fake it with a TreeMap?!(T,HashSet?).
  • std::deque -- This is basically a std::vector that allows O(1) insertion and removal off the head as well as the tail. Don't think Tango has anything like this. You might be able to use LinkSeq?, but std::deque guarantees O(1) random access which LinkSeq? doesn't have.
  • std::slist -- This is a singly linked list. Mostly for use where memory is too tight for the doubly-linked std::list. Don't think Tango has this...
  • std::bit_vector -- BitArray?

See SGI's STL page for more details about what guarantees each container type provides.

Tango also has a few things that STL doesn't, like CircularSeq.

Posted: 09/23/07 19:25:55

Both Dalek and baxissimo should now be able to edit the wiki :)