1

On Overload Jouranl for this month I found an article about how to design Observer pattern with C++11. You'll find it here. The reading is interesting but I found a piece of code using std::atomic I don't really understand.

What is the meaning of function next below? It doesn't even seem a function declaration (no return keyword). I am using g++ 4.7.2

#include <iostream>
#include <cstddef>
#include <atomic>

struct ListItem {
 ListItem() {}
 ...
 atomic<ListItem*> next{nullptr};
 ~ListItem() { delete next.load(); }
 };
Abruzzo Forte e Gentile
  • 14,423
  • 28
  • 99
  • 173

1 Answers1

4

That isn't a function but a member declaration with a non-static data member initializer.

atomic<ListItem*> next{nullptr};

In a constructor that does not initialize next, next will automatically be initialized with nullptr. The initialization is done via list-initialization which was introduced in C++11 (together with non-static data member initializers - the latter partly depends on the former).

GCC 4.7.x should support this.

Community
  • 1
  • 1
Columbo
  • 60,038
  • 8
  • 155
  • 203