17

It is well known that both C++ takes features from C but that C also standardizes C++ features. C1x has gained full expression temporaries (previously it only had sequence point temporaries). C1x also took from the C++11 threading effort.

I wonder what other features C1x took from C++?

Johan Bezem
  • 2,582
  • 1
  • 20
  • 47
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212

2 Answers2

8

Some similarities include:

  • static assertions: _Static_assert ( constant-expression , string-literal );
  • atomic support
  • unicode support - adds some typedefs (e.g. char16_t=uint_least16_t), literals, and utilities.
  • _Generic

_Generic is really more like overloading than what we have with the power of templates. The ubiquitous example of _Generic is:

#define cbrt(X) _Generic((X), long double: cbrtl, \
                              default: cbrt, \
                              float: cbrtf)(X) 

..but I'm not sure which were inherited expressly from C++.

justin
  • 104,054
  • 14
  • 179
  • 226
  • *generics*? You mean template? – Nawaz Dec 06 '11 at 08:58
  • 3
    @Nawaz: no, it's called generics, and it's an ugly feature meant to be used where function overloading is used in C++ for years. E.g. if you want to write `sin(x)` to call the correct `sin` depending on whether `x` is `float`, `double` or `long double`. – Yakov Galka Dec 06 '11 at 09:02
  • @Nawaz ybungalobill answered this well =) but I will throw an example up to reduce confusion. – justin Dec 06 '11 at 09:07
  • @ybungalobill: Thanks for the mini introduction. Can C1x's generic be used for generic implementation of data structures, such as Stack, Queue etc? – Nawaz Dec 06 '11 at 09:10
  • @Nawaz: No. They are not templates. – Yakov Galka Dec 06 '11 at 09:13
  • They can be used to fake explicit specializations of `void*`-based data structures. With lots and lots of macros. – R. Martinho Fernandes Dec 06 '11 at 09:15
  • `_Generic` doesn't have anything to do with C++. For example, C has tgmath.h since before. – u0b34a0f6ae Dec 06 '11 at 10:29
  • @kaizer.se i wouldn't call tgmath first class or user extensible support of the same features. tgmath is more for fortran compatibility, and are implementation defined (e.g. via compiler extensions). `_Generic` is standardised, compiler independent, user defined overloading (and such). – justin Dec 06 '11 at 10:50
  • 4
    As you see in your example, with `_Generic`, tgmath.h can be implemented in ISO C again. Anyway, it's not taken from C++ at all, unlike for example atomics which were co-developed with both C++ and C1x in mind. – u0b34a0f6ae Dec 06 '11 at 11:53
6

The threading part of C1x (5.1.2.4) is taken almost literally from C++11. Terms like "conflict" and "atomic operations" have identical definitions, for all practical purposes.

Alignment is also inspired by C++11: the keyword is alignof, and the maximum aligned type is max_align_t (dropping the std:: namespace, of course).

MSalters
  • 173,980
  • 10
  • 155
  • 350