加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 使用clang对std :: atomic函数进行不明确的调用

发布时间:2020-12-16 07:15:14 所属栏目:百科 来源:网络整理
导读:我试图用clang编译我的代码,我之前使用的是g. 我收到错误编译以下代码: #include atomictypedef void (*my_func) ();int main(int argc,char** argv){ std::atomicmy_func _func; _func(); return 0;} 错误是: a.cpp:23:3: error: call to object of type
我试图用clang编译我的代码,我之前使用的是g.

我收到错误编译以下代码:

#include <atomic>

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

错误是:

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.

这不是我的代码,它是我需要保留的遗留代码.在真正的代码中,_func是一个类成员,并且有一个setter和一个getter,根据我的理解,他打算保护它,以便在打算调用它时不会被修改.

编辑:
我使用clang3.6(clang3.7上的相同错误)和g和std :: atomic 4.8.

解决方法

如果问题是“如何在CLang上编译代码”,答案很简单:

#include <atomic>

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

在原子类型上没有定义operator(),因此编译器必须执行类型转换 – 并且有两个选项.另一个修复是使_func volatile:volatile std :: atomic< my_func> _func;,但这不太可读和明显.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读