Victor
Victor

Reputation: 348

Ambiguous call to std::atomic function using clang

I am attempting to compile my code with clang, I was previously using g++.

I am getting errors compiling the following code:

#include <atomic>

typedef void (*my_func) ();
int main(int argc, char** argv)
{
  std::atomic<my_func> _func;
  _func();
  return 0;
}

The error is:

a.cpp:23:3: error: call to object of type 'std::atomic<my_func>' is ambiguous
  _func();
  ^~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/atomic:304:7: note: conversion candidate of type 'void (*)()'
      operator __pointer_type() const noexcept
      ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/atomic:307:7: note: conversion candidate of type 'void (*)()'
      operator __pointer_type() const volatile noexcept
      ^
1 error generated.

This is not my code, it's legacy code that I need to mantain. In the real code _func is a class member and has a setter and a getter, and from my understanding he intends to protect it so it is not modified when he intends to call it.

Edit: I am using clang3.6 (same error on clang3.7) and g++ and std::atomic 4.8.

Upvotes: 4

Views: 932

Answers (2)

SergeyA
SergeyA

Reputation: 62603

If the question is 'how to compile the code on CLang' the answer is simple:

#include <atomic>

typedef void (*my_func) (int );
int main()
{
  std::atomic<my_func> _func;
  (*_func)(42);
  return 0;
}

There is no operator() defined on atomic type, so compiler has to perform a type conversion - and there are two options. Another fix is to make _func volatile: volatile std::atomic<my_func> _func;, but that is much less readable and obvious.

Upvotes: 1

DennisS
DennisS

Reputation: 263

If you are able to edit these sources I would recommend you to replace the function call with something like _func.load()();. This code does essentially the same thing but allows you to avoid the ambiguous call.

The same issue present in MS's Visual C++ compiler. I guess it is general problem with atomic interface design.

Upvotes: 0

Related Questions