D and C++0x
C++ is undergoing an upgrade to a new standard, commonly referred to as C++0x. This article covers the highlights of C++0x core language changes and compares them with what is available in D. Since C++0x is far from being finalized, this article is subject to being obsoleted by new proposals and revisions to existing ones. C++0x standard library changes are not addressed here. TBD means To Be Determined.
Proposed C++0x Features
- A Proposal to Add an Rvalue Reference to the C++ Language
- Clarification of Initialization of Class Objects by rvalues
- Extending Move Semantics To *this (Revision 2)
- static_assert
- Template aliases for C++
- Extern template
- Variadic Templates
- Extending Variadic Template Template Parameters
- A name for the null pointer: nullptr
- Strongly Typed Enums
- Extended friend Declarations
- Generalized Constant Expressions
- Namespace Association ("Strong Using")
- Synchronizing the C++ preprocessor with C99
- Adding Alignment Support to the C++ Programming Language
- Conditionally-Supported Behavior
- Changing Undefined Behavior into Diagnosable Errors
- Adding the long long type to C++
- Adding extended integer types to C++
- Delegating Constructors
- Explicit Conversion Operators
- New Character Types in C++
- Right Angle Brackets
- Deducing the type of variable from its initializer expression
- The Syntax of auto Declarations
- Inheriting Constructors
- A finer-grained alternative to sequence points
- (monomorphic) Lambda expressions and closures for C++
- Proposed addition of __func__ predefined identifier from C99
- Atomic operations with multi-threaded environments
- Sequencing and the concurrency memory model
- Raw String Literals
- PODs unstrung
- Propagating exceptions when joining threads
- Decltype
- Extending sizeof
- UTF8 Literals
- Universal Character Names in Literals
- Defaulted and Deleted Functions
- Unrestricted Unions
- A Multi-threading Library for Standard C++
- Abandoning a Process
- New function declaration syntax for deduced return types
- Allow atomics use in signal handlers
- Making Local Classes more Useful
- Initializer lists
- Thread-Local Storage
- Member Initializers
- Concepts (unified proposal)
- Proposal for new for-loop
- General Attributes for C++
- Extensible Literals
- Dynamic initialization and concurrency
- Minimal Support for Garbage Collection and Reachability-Based Leak Detection
- Forward declaration of enumerations
A Proposal to Add an Rvalue Reference to the C++ Language
N1770: TBD
Clarification of Initialization of Class Objects by rvalues
N1610: TBD
Extending Move Semantics To *this (Revision 2)
N2439: TBD
static_assert
N1720: static assert is part of D.
Template aliases for C++
N2258: Both templates and template instantiations can be aliased:
struct S(T) { T int; }
alias S X; // alias template
alias S!(int) Y; // alias template instantiation
X!(int) x;
Y y; // x and y are the same type
Extern template
N1987: This is a workaround for problems in the traditional compile/link build model. The D compiler deals with this by if multiple modules are compiled at the same time, only one instance of a template is generated for all the generated object files rather than one instance in each object file. Further improvements are planned for generating library modules that avoid multiple redundant template instantiations.
Variadic Templates
N2242: D's variadic templates.
Extending Variadic Template Template Parameters
N2555: TBD
A name for the null pointer: nullptr
N2431: D has the null equivalent.
Strongly Typed Enums
- Comparisons between different enum types should be an error,
but is not:
void main() { enum Color { ClrRed, ClrOrange, ClrYellow, ClrGreen, ClrBlue, ClrViolet }; enum Alert { CndGreen, CndYellow, CndRed }; Color c = Color.ClrRed; Alert a = Alert.CndGreen; a = c; // error a = Color.ClrYellow; // error bool armWeapons = ( a >= Color.ClrYellow ); // ok; oops }
- The underlying type can be specified.
- Named enums are strongly scoped. Anonymous enum members are declared in the enclosing scope.
- Explicit qualification is needed to specify a named enum member.
Extended friend Declarations
N1791: All code in a module has access to private members of any struct or class declared in that module that is in scope. Package protected members can be accessed by any code in the same package. There is no need in D to have friend declarations or complex lookup rules for them.
Generalized Constant Expressions
N2235: D has compile time function execution (CTFE). CTFE is much more flexible, as functions to be evaluated at compile time:
- do not require a special keyword (C++0x requires constexpr)
- can have multiple statements in the function
- can be recursive
- can modify local variables
- can have out parameters
Namespace Association ("Strong Using")
N2535: D doesn't have namespaces, so this is irrelevant.
Synchronizing the C++ preprocessor with C99
N1653: D does not have a preprocessor, so this is not relevant to D.
Adding Alignment Support to the C++ Programming Language
N2341: D has the align attribute to specify the alignment of declarations, and the .alignof property to determine the alignment size of an expression or type.
Conditionally-Supported Behavior
N1627: There are some allowed vendor specific behaviors in D, such as pragmas.
Changing Undefined Behavior into Diagnosable Errors
N1727: D does not have undefined behavior with integer literal types, character escapes, or passing non-POD objects to ellipses.
Adding the long long type to C++
N1811: D's long type is equivalent.
Adding extended integer types to C++
N1988: D has the cent and ucent types for 128 bit integral types (not implemented in dmd or gdc). There is no proposal for other extended integral types, but it's hard to imagine a justification for adding more to the core language.
Delegating Constructors
N1986: D has delegating constructors.
Explicit Conversion Operators
N2437: TBD
New Character Types in C++
N2249: C++0x adds new character types char16_t and char32_t, which are equivalent to D's wchar and dchar types. The u and U character literal prefixes are equivalent to the D w and d postfixes.
Right Angle Brackets
N1757: Since D uses !( ) to instantiate templates rather than < >, there are no parsing ambiguities and no fixes are necessary.
Deducing the type of variable from its initializer expression
N1984: D has type inference from initializers.
The Syntax of auto Declarations
N2546: D auto declarations do not have syntactic issues.
Inheriting Constructors
N2540: TBD
A finer-grained alternative to sequence points
N2239: TBD
(monomorphic) Lambda expressions and closures for C++
N2550: D has FunctionLiterals (lambda expressions) and closures.
Proposed addition of __func__ predefined identifier from C99
N2340: TBD
Atomic operations with multi-threaded environments
N2427: TBD
Sequencing and the concurrency memory model
N2429: TBD
Raw String Literals
N2442: D has wysiwyg and delimited strings, and all strings are Unicode.
PODs unstrung
N2294: All D structs are POD (Plain Old Data). D classes are reference, polymorphic types.
Propagating exceptions when joining threads
N2179: TBD
Decltype
N2343: The equivalent D construct is typeof.
Extending sizeof
N2253: Using sizeof without a this object:
struct S {
int a;
static int foo() {
return a.sizeof;
}
}
void test() {
int x = S.a.sizeof;
}
works correctly in D.
UTF-8 Literals
N2442: Char string literals are in UTF-8 format.
Universal Character Names in Literals
N2170: All Unicode characters are allowed in string literals. Surrogate pair halves are not allowed unless hex literal notation is used.
Defaulted and Deleted Functions
N2326: D alows individual functions to be marked as disabled.
Unrestricted Unions
N2544: TBD
A Multi-threading Library for Standard C++
N2447: TBD
Abandoning a Process
N2440: TBD
New function declaration syntax for deduced return types
N2445: TBD
Allow atomics use in signal handlers
N2547: TBD
Making Local Classes more Useful
N2402: D has no restrictions on using local classes as template parameters.
Initializer lists
N2531: D has struct literals, array literals, and associative array literals.
Thread-Local Storage
N2280: Thread-local storage is the default for statics and globals in D. Thread-global storage is done by using the shared storage class.
Member Initializers
N2426: D has member initializers, which are called default initializers.
Concepts (unified proposal)
N2081: The D equivalent of C++ concepts are constraints. (As of July 2009, Concepts have been dropped from C++0x.)
Description | D Constraints | C++0x Concepts |
---|---|---|
Overloading based on concepts/constraints | Yes | Yes |
Concepts/constraints on template type parameters | Yes | Yes |
Concepts/constraints on template value parameters | Yes | No |
Concepts/constraints on template template/alias parameters | Yes | No |
Composition and refinement of concepts/constraints | Yes | Yes |
Multi-type concepts/constraints | Yes | Yes |
Expression of concepts/constraints | Done with compile time expressions | Done by enumeration of function signatures |
Axioms | Yes (as static asserts and function preconditions) | Yes |
new keywords | No | Yes: where, concept, concept_map, axiom, late_check |
Semantic analysis of template bodies | Lazy (done at instantiation time) | Eager (done at definition time) |
Template body checked against concept/constraint | No | Yes |
Concept maps | No (but could be done with proxy objects) | Yes |
All operations on constrained types must be specified in concept/constraint | No | Yes |
Complexity | if (expression) added to template grammar | quite a bit of new grammar and semantics added |
Proposal for new for-loop
N2394: This is equivalent to the D ForeachRangeStatement.
General Attributes for C++
N2418: Vendor specific attributes can be applied to statements and declarations with pragmas.
Extensible Literals
N2378: D does not have user extensible literals.
Dynamic initialization and concurrency
N2513: TBD
Minimal Support for Garbage Collection and Reachability-Based Leak Detection
N2527: Garbage collection is optional in C++0x, D requires it. The problem with an optional garbage collector is that in order to write general purpose libraries, one must assume that there is no garbage collector, therefore none of the productivity enhancing advantages of it are available.
Forward declaration of enumerations
N2499: Forward declarations are not necessary in D, as all declarations are resolved essentially in parallel. Incomplete enum types, however, are possible:
enum E : int;
where the member values are hidden from the user.