1

I was working on this thread swap c++ map objects in multithreaded environment

However,

#include <memory>
#include <thread>
#include <chrono>
#include <atomic>
#include <iostream>
using namespace std;

shared_ptr<std::string> the_string;

int main()
{
     atomic_store(&the_string, std::make_shared<std::string>("first string"));
}     

gives a compile time error

error: no matching function for call to 'atomic_store'
     atomic_store(&the_string, std::make_shared<std::string>("first string"));
     ^~~~~~~~~~~~
/Library/Developer/CommandLineTools/usr/include/c++/v1/atomic:1165:1: note: candidate template ignored: could not match 'atomic' against 'shared_ptr'
atomic_store(volatile atomic<_Tp>* __o, _Tp __d) _NOEXCEPT

I did see a few threads on this problem and understand that it could be related to C++ version I have /usr/include/c++/4.2.1/ and /usr/include/c++/4.8.5/ on another box, both give the same issue. Should I upgrade the C++ version?

I resolved this issue by passing -std=c++11 flag.

Raj
  • 401
  • 6
  • 20

2 Answers2

1

I resolved this issue by passing the flag -std=c++11

Raj
  • 401
  • 6
  • 20
  • I'd usually recommend `-std=gnu++11` unless you specifically want to disable some GNU extensions. e.g. you have to use `__asm__` instead of `asm` with `-std=c++11`. I can't think of any more-relevant keywords or extensions that are disabled, though, so it's normally fine. – Peter Cordes May 15 '19 at 07:12
0

It compiles fine here with GCC 8.3 and Clang 8.0, so yes, you should upgrade your compiler.

Nikos C.
  • 50,738
  • 9
  • 71
  • 96