Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are the std::atomic_{char,schar,etc.} typedefs allowed to be typedefs to a base class of std::atomic<T>, and not to atomic<T> only?

C++11 [atomics.types.generic]p7:

There shall be named types corresponding to the integral specializations of atomic, as specified in Table 145, and a named type atomic_bool corresponding to the specified atomic<bool>. Each named type is a either typedef to the corresponding specialization or a base class of the corresponding specialization. If it is a base class, it shall support the same member functions as the corresponding specialization.

(emphasis added)

What's the rationale for letting std::atomic_char not be identical to std::atomic<char>, and so on for all the other types? What flexibility does this enable, and why is it useful? At first glance I don't understand why these wouldn't be required to be typedefs to std::atomic<T> specializations directly.

Note that N2427 which proposed <atomic> says that the typedefs are to specializations exactly, and not possibly to base classes.

like image 632
Jeff Walden Avatar asked May 07 '13 16:05

Jeff Walden


1 Answers

It is probably there because someone already had an implementation of atomics using std::atomic_* types as base classes and complained enough/provided a good argument towards this approach.

In MSVC such an implementation is suggested.

In this it is stated that

Remove the definional base-class relationship between atomic_.... named types and the corresponding specializations of the atomic template class. The base-class relationship is now implementation-dependent. This change ensures compatibility with C. Some member functions and operators, that were formerly inherited from the base class, must be hoisted from the named types to the specializations.

like image 50
ipapadop Avatar answered Nov 16 '22 00:11

ipapadop