lundi 11 janvier 2021

can't compile struct with std::atomic member with explicit instantiation

I am exploring the use of std::atomic in a struct across translation units and have run into a constructor compile problem. When I try to use explicit instantiation, the compiler says they don't match. How do I match up the explicit instantiation and the A constructor?

#include <string>
#include <atomic>
#include <map>

struct A
{
  A( std::string strArg, bool onOffArg ) // added constuctor after compiler complained it couldn't find one that matched
    : str { strArg }, onOff { onOffArg } {}
  ~A() {}

  std::string str {};
  std::atomic< bool >( onOff ) { false }; // error C2440: 'initializing': cannot convert from 'initializer list' to 'std::map<int,A,std::less<int>,std::allocator<std::pair<const int,A>>>', 'No constructor could take the source type, or constructor overload resolution was ambiguous'
  //bool onOff; // OK
};



int main()
{
  std::map< int, A > aMap
  {
    { 1, { "One", false } } // assuming inner braces are a match for A ctor
  };
}

Aucun commentaire:

Enregistrer un commentaire