6

This is the declaration I have in .h file:

static std::atomic<int> OrdersExecutorIdCounter;

This is initilization from .cpp file:

std::atomic<int> ActionBasedOrdersExecutor::OrdersExecutorIdCounter = 0;

It compiles just fine in VC++, but in gcc 4.8 I get this error:

error: use of deleted function ‘std::atomic<int>::atomic(const std::atomic<int>&)’

How can I solve this problem?

Oleg Vazhnev
  • 23,239
  • 54
  • 171
  • 305
  • Show the exact compilation command. – Basile Starynkevitch Sep 19 '14 at 19:00
  • Think about what this code does in theory. Hint: copy-initialization makes a difference. – chris Sep 19 '14 at 19:04
  • `g++ -std=c++0x -I"/home/oleg/eclipseWorkspace/CommonsLibrary" -O3 -Wall -c -fmessage-length=0 -MMD -MP -MF"ActionBasedOrdersExecutor.d" -MT"ActionBasedOrdersExecutor.d" -o "ActionBasedOrdersExecutor.o" "../ActionBasedOrdersExecutor.cpp"`, i've removed some `-I` as not important – Oleg Vazhnev Sep 19 '14 at 19:04
  • @javapowered - Possible (similar) duplicated: https://stackoverflow.com/q/27314485/1971003 – Guy Avraham Dec 11 '17 at 19:31
  • Duplicated https://stackoverflow.com/questions/27314485/ – Joma Jan 31 '19 at 05:05

2 Answers2

14

You can directly initialise the atomic variable, which does not require deleted copy constructor, eg:

std::atomic<int> ActionBasedOrdersExecutor::OrdersExecutorIdCounter{0};
Rafal Mielniczuk
  • 1,322
  • 7
  • 11
1

You don't need (or want) to initialize your atomic integer to 0 (the int value would be 0 initialized for a global).

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547