Ddoc $(COMMUNITY D and C++0x, $(P C++ is undergoing an upgrade to a new standard, commonly referred to as C++0x. This article covers the highlights of $(LINK2 $(NDOCS)2008/n2597.html, 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. ) $(SECTION3 Proposed C++0x Features, $(UL $(LI $(LINK2 #rvalue-reference, A Proposal to Add an Rvalue Reference to the C++ Language)) $(LI $(LINK2 #class-rvalue, Clarification of Initialization of Class Objects by rvalues)) $(LI $(LINK2 #move-semantics, Extending Move Semantics To *this (Revision 2))) $(LI $(LINK2 #static-assert, static_assert)) $(LI $(LINK2 #template-aliases, Template aliases for C++)) $(LI $(LINK2 #extern-template, Extern template)) $(LI $(LINK2 #variadic-templates, Variadic Templates)) $(LI $(LINK2 #variadic-template-templates, Extending Variadic Template Template Parameters)) $(LI $(LINK2 #nullptr, A name for the null pointer: nullptr)) $(LI $(LINK2 #strong-enums, Strongly Typed Enums)) $(LI $(LINK2 #extended-friends, Extended friend Declarations)) $(LI $(LINK2 #constant-expressions, Generalized Constant Expressions)) $(LI $(LINK2 #namespace-association, Namespace Association ("Strong Using"))) $(LI $(LINK2 #c99-preprocessor, Synchronizing the C++ preprocessor with C99)) $(LI $(LINK2 #alignment, Adding Alignment Support to the C++ Programming Language)) $(LI $(LINK2 #conditional, Conditionally-Supported Behavior)) $(LI $(LINK2 #undefined-behavior, Changing Undefined Behavior into Diagnosable Errors)) $(LI $(LINK2 #long-long, Adding the long long type to C++)) $(LI $(LINK2 #extended-integer-types, Adding extended integer types to C++)) $(LI $(LINK2 #delegating-ctors, Delegating Constructors)) $(LI $(LINK2 #explicit-conversion, Explicit Conversion Operators)) $(LI $(LINK2 #char16_t, New Character Types in C++)) $(LI $(LINK2 #right-angle-brackets, Right Angle Brackets)) $(LI $(LINK2 #type-deduction, Deducing the type of variable from its initializer expression)) $(LI $(LINK2 #auto-declarations, The Syntax of auto Declarations)) $(LI $(LINK2 #inheriting-ctors, Inheriting Constructors)) $(LI $(LINK2 #sequence-points, A finer-grained alternative to sequence points)) $(LI $(LINK2 #lambda, (monomorphic) Lambda expressions and closures for C++)) $(LI $(LINK2 #__func__, Proposed addition of __func__ predefined identifier from C99)) $(LI $(LINK2 #atomic, Atomic operations with multi-threaded environments)) $(LI $(LINK2 #concurrency, Sequencing and the concurrency memory model)) $(LI $(LINK2 #raw-strings, Raw String Literals)) $(LI $(LINK2 #pod, PODs unstrung)) $(LI $(LINK2 #exceptions, Propagating exceptions when joining threads)) $(LI $(LINK2 #decltype, Decltype)) $(LI $(LINK2 #sizeof, Extending sizeof)) $(LI $(LINK2 #utf8-literals, UTF8 Literals)) $(LI $(LINK2 #ucs-in-literals, Universal Character Names in Literals)) $(LI $(LINK2 #defaulted, Defaulted and Deleted Functions)) $(LI $(LINK2 #unions, Unrestricted Unions)) $(LI $(LINK2 #library, A Multi-threading Library for Standard C++)) $(LI $(LINK2 #abandoning, Abandoning a Process)) $(LI $(LINK2 #return-types, New function declaration syntax for deduced return types)) $(LI $(LINK2 #atomic-signals, Allow atomics use in signal handlers)) ) $(UL $(LI $(LINK2 #local-classes, Making Local Classes more Useful)) $(LI $(LINK2 #initializer-lists, Initializer lists)) $(LI $(LINK2 #thread-local-storage, Thread-Local Storage)) $(LI $(LINK2 #member-initializers, Member Initializers)) $(LI $(LINK2 #concepts, Concepts (unified proposal))) $(LI $(LINK2 #for-loop, Proposal for new for-loop)) $(LI $(LINK2 #general-attributes, General Attributes for C++)) ) $(UL $(LI $(LINK2 #extensible-literals, Extensible Literals)) $(LI $(LINK2 #dynamic-concurrency, Dynamic initialization and concurrency)) $(LI $(LINK2 #garbage-collection, Minimal Support for Garbage Collection and Reachability-Based Leak Detection)) $(LI $(LINK2 #forward-enums, Forward declaration of enumerations)) ) ) $(SECTION3 $(LNAME2 rvalue-reference, A Proposal to Add an Rvalue Reference to the C++ Language), $(P $(LINK2 $(NDOCS)2005/n1770.html, N1770): TBD ) ) $(SECTION3 $(LNAME2 class-rvalue, Clarification of Initialization of Class Objects by rvalues), $(P $(LINK2 $(NDOCS)2004/n1610.html, N1610): TBD ) ) $(SECTION3 $(LNAME2 move-semantics, Extending Move Semantics To *this (Revision 2)), $(P $(LINK2 $(NDOCS)2007/n2439.htm, N2439): TBD ) ) $(SECTION3 $(LNAME2 static-assert, static_assert), $(P $(LINK2 $(NDOCS)2004/n1720.html, N1720): $(LINK2 version.html#StaticAssert, static assert) is part of D. ) ) $(SECTION3 $(LNAME2 template-aliases, Template aliases for C++), $(P $(LINK2 $(NDOCS)2007/n2258.pdf, N2258): Both $(LINK2 template.html, templates and template instantiations) can be $(LINK2 declaration.html#alias, 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 --- ) $(SECTION3 $(LNAME2 extern-template, Extern template), $(P $(LINK2 $(NDOCS)2006/n1987.htm, 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. ) ) $(SECTION3 $(LNAME2 variadic-templates, Variadic Templates), $(P $(LINK2 $(NDOCS)2007/n2242.pdf, N2242): D's $(LINK2 template.html#variadic-templates, variadic templates). ) ) $(SECTION3 $(LNAME2 variadic-template-templates, Extending Variadic Template Template Parameters), $(P $(LINK2 $(NDOCS)2008/n2555.pdf, N2555): TBD ) ) $(SECTION3 $(LNAME2 nullptr, A name for the null pointer: nullptr), $(P $(LINK2 $(NDOCS)2007/n2431.pdf, N2431): D has the $(LINK2 expression.html#null, null) equivalent. ) ) $(SECTION3 $(LNAME2 strong-enums, Strongly Typed Enums), $(P $(LINK2 $(NDOCS)2007/n2347.pdf, N2347): D $(LINK2 enum.html, enums) are: ) $(OL $(LI 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 } --- ) $(LI The underlying type can be specified.) $(LI Named enums are strongly scoped. Anonymous enum members are declared in the enclosing scope.) $(LI Explicit qualification is needed to specify a named enum member.) ) ) $(SECTION3 $(LNAME2 extended-friends, Extended friend Declarations), $(P $(LINK2 $(NDOCS)2005/n1791.pdf, N1791): All code in a module has $(LINK2 attribute.html#ProtectionAttribute, 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. ) ) $(SECTION3 $(LNAME2 constant-expressions, Generalized Constant Expressions), $(P $(LINK2 $(NDOCS)2007/n2235.pdf, N2235): D has $(LINK2 function.html#interpretation, compile time function execution) (CTFE). CTFE is much more flexible, as functions to be evaluated at compile time: ) $(OL $(LI do not require a special keyword (C++0x requires $(CODE constexpr))) $(LI can have multiple statements in the function) $(LI can be recursive) $(LI can modify local variables) $(LI can have out parameters) ) ) $(SECTION3 $(LNAME2 namespace-association, Namespace Association ("Strong Using")), $(P $(LINK2 $(NDOCS)2008/n2535.html, N2535): D doesn't have namespaces, so this is irrelevant. ) ) $(SECTION3 $(LNAME2 c99-preprocessor, Synchronizing the C++ preprocessor with C99), $(P $(LINK2 $(NDOCS)2004/n1653.htm, N1653): D does not have a preprocessor, so this is not relevant to D. ) ) $(SECTION3 $(LNAME2 alignment, Adding Alignment Support to the C++ Programming Language), $(P $(LINK2 $(NDOCS)2007/n2341.pdf, N2341): D has the $(LINK2 attribute.html#align, align) attribute to specify the alignment of declarations, and the $(LINK2 property.html#alignof, .alignof) property to determine the alignment size of an expression or type. ) ) $(SECTION3 $(LNAME2 conditional, Conditionally-Supported Behavior), $(P $(LINK2 $(NDOCS)2004/n1627.pdf, N1627): There are some allowed vendor specific behaviors in D, such as $(LINK2 pragma.html, pragmas). ) ) $(SECTION3 $(LNAME2 undefined-behavior, Changing Undefined Behavior into Diagnosable Errors), $(P $(LINK2 $(NDOCS)2004/n1727.pdf, N1727): D does not have undefined behavior with integer literal types, character escapes, or passing non-POD objects to ellipses. ) ) $(SECTION3 $(LNAME2 long-long, Adding the long long type to C++), $(P $(LINK2 $(NDOCS)2005/n1811.pdf, N1811): D's $(LINK2 type.html, long) type is equivalent. ) ) $(SECTION3 $(LNAME2 extended-integer-types, Adding extended integer types to C++), $(P $(LINK2 $(NDOCS)2006/n1988.pdf, N1988): D has the $(LINK2 type.html, 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. ) ) $(SECTION3 $(LNAME2 delegating-ctors, Delegating Constructors), $(P $(LINK2 $(NDOCS)2006/n1986.pdf, N1986): D has $(LINK2 class.html#delegating-constructors, delegating constructors). ) ) $(SECTION3 $(LNAME2 explicit-conversion, Explicit Conversion Operators), $(P $(LINK2 $(NDOCS)2007/n2437.html, N2437): TBD ) ) $(SECTION3 $(LNAME2 char16_t, New Character Types in C++), $(P $(LINK2 $(NDOCS)2007/n2249.html, N2249): C++0x adds new character types char16_t and char32_t, which are equivalent to D's $(LINK2 type.html, wchar and dchar types). The u and U character literal prefixes are equivalent to the D $(LINK2 lex.html#StringLiteral, w and d postfixes). ) ) $(SECTION3 $(LNAME2 right-angle-brackets, Right Angle Brackets), $(P $(LINK2 $(NDOCS)2005/n1757.html, N1757): Since D uses !( ) to instantiate templates rather than < >, there are no parsing ambiguities and no fixes are necessary. ) ) $(SECTION3 $(LNAME2 type-deduction, Deducing the type of variable from its initializer expression), $(P $(LINK2 $(NDOCS)2006/n1984.pdf, N1984): D has $(LINK2 declaration.html#AutoDeclaration, type inference) from initializers. ) ) $(SECTION3 $(LNAME2 auto-declarations, The Syntax of auto Declarations), $(P $(LINK2 $(NDOCS)2008/n2546.html, N2546): D $(LINK2 attribute.html#auto, auto declarations) do not have syntactic issues. ) ) $(SECTION3 $(LNAME2 inheriting-ctors, Inheriting Constructors), $(P $(LINK2 $(NDOCS)2008/n2540.html, N2540): TBD ) ) $(SECTION3 $(LNAME2 sequence-points, A finer-grained alternative to sequence points), $(P $(LINK2 $(NDOCS)2007/n2239.htm, N2239): TBD ) ) $(SECTION3 $(LNAME2 lambda, (monomorphic) Lambda expressions and closures for C++), $(P $(LINK2 $(NDOCS)2008/n2550.pdf, N2550): D has $(LINK2 expression.html#FunctionLiteral, lambda expressions) and $(LINK2 function.html#closures, closures). ) ) $(SECTION3 $(LNAME2 __func__, Proposed addition of __func__ predefined identifier from C99), $(P $(LINK2 $(NDOCS)2007/n2340.html, N2340): TBD ) ) $(SECTION3 $(LNAME2 atomic, Atomic operations with multi-threaded environments), $(P $(LINK2 $(NDOCS)2007/n2427.html, N2427): TBD ) ) $(SECTION3 $(LNAME2 concurrency, Sequencing and the concurrency memory model), $(P $(LINK2 $(NDOCS)2007/n2429.htm, N2429): TBD ) ) $(SECTION3 $(LNAME2 raw-strings, Raw String Literals), $(P $(LINK2 $(NDOCS)2007/n2442.html, N2442): D has $(LINK2 lex.html#StringLiteral, wysiwyg and delimited strings), and all strings are Unicode. ) ) $(SECTION3 $(LNAME2 pod, PODs unstrung), $(P $(LINK2 $(NDOCS)2007/n2294.html, N2294): All D $(LINK2 struct.html, structs) are $(LINK2 glossary.html#pod, POD (Plain Old Data)). D $(LINK2 class.html, classes) are reference, polymorphic types. ) ) $(SECTION3 $(LNAME2 exceptions, Propagating exceptions when joining threads), $(P $(LINK2 $(NDOCS)2007/n2179.html, N2179): TBD ) ) $(SECTION3 $(LNAME2 decltype, Decltype), $(P $(LINK2 $(NDOCS)2007/n2343.pdf, N2343): The equivalent D construct is $(LINK2 declaration.html#typeof, typeof). ) ) $(SECTION3 $(LNAME2 sizeof, Extending sizeof), $(P $(LINK2 $(NDOCS)2007/n2253.html, N2253): Using $(LINK2 property.html#sizeof, sizeof) without a $(I this) object: --- struct S { int a; static int foo() { return a.sizeof; } } void test() { int x = S.a.sizeof; } --- works correctly in D. ) ) $(SECTION3 $(LNAME2 utf8-literals, UTF-8 Literals), $(P $(LINK2 $(NDOCS)2007/n2442.html, N2442): $(LINK2 lex.html#StringLiteral, Char string literals) are in UTF-8 format. ) ) $(SECTION3 $(LNAME2 ucs-in-literals, Universal Character Names in Literals), $(P $(LINK2 $(NDOCS)2007/n2170.html, N2170): All Unicode characters are allowed in $(LINK2 lex.html#StringLiteral, string literals). Surrogate pair halves are not allowed unless hex literal notation is used. ) ) $(SECTION3 $(LNAME2 defaulted, Defaulted and Deleted Functions), $(P $(LINK2 $(NDOCS)/2007/n2326.html#delete, N2326): D alows individual functions to be marked as $(LINK2 attribute.html#disable, disabled). ) ) $(SECTION3 $(LNAME2 unions, Unrestricted Unions), $(P $(LINK2 $(NDOCS)2008/n2544.pdf, N2544): TBD ) ) $(SECTION3 $(LNAME2 library, A Multi-threading Library for Standard C++), $(P $(LINK2 $(NDOCS)2007/n2447.html, N2447): TBD ) ) $(SECTION3 $(LNAME2 abandoning, Abandoning a Process), $(P $(LINK2 $(NDOCS)2007/n2440.html, N2440): TBD ) ) $(SECTION3 $(LNAME2 return-types, New function declaration syntax for deduced return types), $(P $(LINK2 $(NDOCS)2007/n2445.html, N2445): TBD ) ) $(SECTION3 $(LNAME2 atomic-signals, Allow atomics use in signal handlers), $(P $(LINK2 $(NDOCS)2008/n2547.htm, N2547): TBD ) ) $(SECTION3 $(LNAME2 local-classes, Making Local Classes more Useful), $(P $(LINK2 $(NDOCS)2007/n2402.pdf, N2402): D has no restrictions on using local classes as template parameters. ) ) $(SECTION3 $(LNAME2 initializer-lists, Initializer lists), $(P $(LINK2 $(NDOCS)2008/n2531.pdf, N2531): D has $(LINK2 struct.html#StructLiteral, struct literals), $(LINK2 expression.html#ArrayLiteral, array literals), and $(LINK2 expression.html#AssocArrayLiteral, associative array literals). ) ) $(SECTION3 $(LNAME2 thread-local-storage, Thread-Local Storage), $(P $(LINK2 $(NDOCS)2007/n2280.html, N2280): Thread-local storage is the default for statics and globals in D. Thread-global storage is done by using the $(LINK2 migrate-to-shared.html, $(B shared)) storage class. ) ) $(SECTION3 $(LNAME2 member-initializers, Member Initializers), $(P $(LINK2 $(NDOCS)2007/n2426.htm, N2426): D has $(LINK2 class.html#class-default-initializer, member initializers), which are called $(I default initializers). ) ) $(SECTION3 $(LNAME2 concepts, Concepts (unified proposal)), $(P $(LINK2 $(NDOCS)2006/n2081.pdf, N2081): The D equivalent of C++ concepts are $(LINK2 concepts.html, constraints). (As of $(LINK2 http://www.ddj.com/cpp/218600111, July 2009), Concepts have been dropped from C++0x.) ) $(TABLE2 Concepts and Constraints, $(TR $(TH Description) $(TH D Constraints) $(TH C++0x Concepts) ) $(TR $(TD Overloading based on concepts/constraints) $(TD Yes) $(TD Yes) ) $(TR $(TD Concepts/constraints on template type parameters) $(TD Yes) $(TD Yes) ) $(TR $(TD Concepts/constraints on template value parameters) $(TD Yes) $(TD No) ) $(TR $(TD Concepts/constraints on template template/alias parameters) $(TD Yes) $(TD No) ) $(TR $(TD Composition and refinement of concepts/constraints) $(TD Yes) $(TD Yes) ) $(TR $(TD Multi-type concepts/constraints) $(TD Yes) $(TD Yes) ) $(TR $(TD Expression of concepts/constraints) $(TD Done with compile time expressions) $(TD Done by enumeration of function signatures) ) $(TR $(TD Axioms) $(TD Yes (as static asserts and function preconditions)) $(TD Yes) ) $(TR $(TD new keywords) $(TD No) $(TD Yes: where, concept, concept_map, axiom, late_check) ) $(TR $(TD Semantic analysis of template bodies) $(TD Lazy (done at instantiation time)) $(TD Eager (done at definition time)) ) $(TR $(TD Template body checked against concept/constraint) $(TD No) $(TD Yes) ) $(TR $(TD Concept maps) $(TD No (but could be done with proxy objects)) $(TD Yes) ) $(TR $(TD All operations on constrained types must be specified in concept/constraint) $(TD No) $(TD Yes) ) $(TR $(TD Complexity) $(TD $(CODE if (expression)) added to template grammar) $(TD quite a bit of new grammar and semantics added) ) ) ) $(SECTION3 $(LNAME2 for-loop, Proposal for new for-loop), $(P $(LINK2 $(NDOCS)2007/n2394.html, N2394): This is equivalent to the D $(LINK2 statement.html#ForeachRangeStatement, $(I ForeachRangeStatement)). ) ) $(SECTION3 $(LNAME2 general-attributes, General Attributes for C++), $(P $(LINK2 $(NDOCS)2007/n2418.pdf, N2418): Vendor specific attributes can be applied to statements and declarations with $(LINK2 pragma.html, pragmas). ) ) $(SECTION3 $(LNAME2 extensible-literals, Extensible Literals), $(P $(LINK2 $(NDOCS)2007/n2378.pdf, N2378): D does not have user extensible literals. ) ) $(SECTION3 $(LNAME2 dynamic-concurrency, Dynamic initialization and concurrency), $(P $(LINK2 $(NDOCS)2008/n2513.html, N2513): TBD ) ) $(SECTION3 $(LNAME2 garbage-collection, Minimal Support for Garbage Collection and Reachability-Based Leak Detection), $(P $(LINK2 $(NDOCS)2008/n2527.html, N2527): Garbage collection is optional in C++0x, D $(LINK2 garbage.html, 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. ) ) $(SECTION3 $(LNAME2 forward-enums, Forward declaration of enumerations), $(P $(LINK2 $(NDOCS)2008/n2499.pdf, N2499): Forward declarations are not necessary in D, as all declarations are resolved essentially in parallel. Incomplete $(LINK2 enum.html, enum) types, however, are possible: ) --- enum E : int; --- $(P where the member values are hidden from the user. ) ) ) Macros: TITLE=D and C++0x WIKI=Cpp0x DOLLAR=$ NDOCS=http://www.open-std.org/jtc1/sc22/wg21/docs/papers/ FOO=