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
- tango.io.Print also provides useful functions
- for advanced logging, there is tango.util.log (see tutorial)
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