c++ std::atomic<bool>::fetch_or 没有实现?
c++ std::atomic<bool>::fetch_or not implemented?
使用这段代码摘录:
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
:
,它会编译
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 参考资料说 "the full specialization atomic<bool>
" 被视为 "non-specialized",我认为这是问题的根源。所以我的疑惑:
- "full specialization" 怎么会是 "treated as non-specialized"?
- 在调用
flag.fetch_or()
时,使用 as flag 模板参数 int
而不是 bool
是否会遇到任何棘手的陷阱?
我正在使用 gcc 5.1.0,并使用 -std=c++14
进行编译。
C++11 N3337 draft 不需要 bool
的方法。
29.5 "Atomic types"
template <class T> struct atomic {
[...]
}
template <> struct atomic<integral> {
[...]
integral fetch_or(integral , memory_order = memory_order_seq_cst) noexcept;
[...]
}
29.5/1:
The semantics of the operations on specializations of atomic are defined in 29.6.
29.6.3/2 "Arithmetic operations on atomic types":
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.
和Table145不包含bool
.
所以只有 struct
的积分(没有 bool
)特化会有那个方法。
这有点令人困惑,因为在标准的其余部分,"integral types" 包括 bool、3.9.1/7 "Fundamental types":
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.
使用这段代码摘录:
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
:
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 参考资料说 "the full specialization atomic<bool>
" 被视为 "non-specialized",我认为这是问题的根源。所以我的疑惑:
- "full specialization" 怎么会是 "treated as non-specialized"?
- 在调用
flag.fetch_or()
时,使用 as flag 模板参数int
而不是bool
是否会遇到任何棘手的陷阱?
我正在使用 gcc 5.1.0,并使用 -std=c++14
进行编译。
C++11 N3337 draft 不需要 bool
的方法。
29.5 "Atomic types"
template <class T> struct atomic {
[...]
}
template <> struct atomic<integral> {
[...]
integral fetch_or(integral , memory_order = memory_order_seq_cst) noexcept;
[...]
}
29.5/1:
The semantics of the operations on specializations of atomic are defined in 29.6.
29.6.3/2 "Arithmetic operations on atomic types":
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.
和Table145不包含bool
.
所以只有 struct
的积分(没有 bool
)特化会有那个方法。
这有点令人困惑,因为在标准的其余部分,"integral types" 包括 bool、3.9.1/7 "Fundamental types":
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.