C ++ std :: atomic< bool> :: fetch_还是未实现? [英] c++ std::atomic<bool>::fetch_or not implemented?

查看:104
本文介绍了C ++ std :: atomic< bool> :: fetch_还是未实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具有以下代码摘录:

class myclass {
    volatile std::atomic<bool> flag;
    public:
    myclass(): flag(false) {}
    bool get_flag() { return flag; }
    bool try_set() {
        return !flag.fetch_or(flag, true);
    }
    void reset() {
        flag = false;
    }
};

我遇到此编译错误:

error: ‘volatile struct std::atomic<bool>’ has no member named ‘fetch_or’   
   return !flag.fetch_or(flag, true);

但是,如果我将模板参数更改为 int,则会进行编译

It compiles if, however, I change the template parameter to int:

class myclass {
    volatile std::atomic<int> flag;
    public:
    myclass(): flag(0) {}
    bool get_flag() { return flag; }
    bool try_set() {
        return !flag.fetch_or(flag, true);
    }
    void reset() {
        flag = 0;
    }
};

atomic 参考文献指出,完全专业化 atomic< bool> 被视为非专业化,相信是问题的根源。所以我的疑问是:

The atomic reference says that "the full specialization atomic<bool>" is treated as "non-specialized", what I believe to be the source of the problems. So my doubts:


  1. 如何将完全专业化视为非专业化?

  2. 使用标志模板参数 int 代替 bool 调用 flag.fetch_or()

  1. How can a "full specialization" be "treated as non-specialized"?
  2. May there I face any tricky pitfalls using as flag template parameter int instead of bool when calling flag.fetch_or()?

我正在使用gcc 5.1.0,并使用 -std = c ++ 14 进行编译。

I am using gcc 5.1.0, and compiling with -std=c++14.

推荐答案

C ++ 11 N3337草案对于 bool 不需要该方法。

29.5原子类型

template <class T> struct atomic {
  [...]
}

template <> struct atomic<integral> {
  [...]
  integral fetch_or(integral , memory_order = memory_order_seq_cst) noexcept;
  [...]
}

29.5 / 1:


关于原子专业化操作的语义在29.6中定义。

The semantics of the operations on specializations of atomic are defined in 29.6.

29.6.3 / 2对原子类型的算术运算:

29.6.3/2 "Arithmetic operations on atomic types":


在这些函数和函数模板的声明中在专业化方面,积分的名称是指整数类型,而名称atomic-integral的名称是原子的,或者是表145或表146推断的积分的命名基类。

In the declarations of these functions and function template specializations, the name integral refers to an integral type and the name atomic-integral refers to either atomic or to a named base class for integral from Table 145 or inferred from Table 146.

并且表145不包含 bool

因此,仅积分(没有 bool )的专业化 struct 将具有该方法。

So only the integral (without bool) specializations of struct will have that method.

这有点令人困惑,因为在标准的其余部分中,整数类型包括布尔值3.9.1 / 7基本类型:

This is a bit confusing because in the rest of the standard, "integral types" includes bool, 3.9.1/7 "Fundamental types":


键入bool,char,char16_t,char32_t,wcha r_t以及有符号和无符号整数类型统称为整数类型。整数类型的同义词是整数类型。

Types bool, char, char16_t, char32_t, wchar_t, and the signed and unsigned integer types are collectively called integral types. A synonym for integral type is integer type.

这篇关于C ++ std :: atomic&lt; bool&gt; :: fetch_还是未实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆