我正在尝试为std :: bitset编写bool转换运算符
我试过了:
template<size_t size>
operator bool(std::bitset<size> & b)
{
return b.any();
}
Run Code Online (Sandbox Code Playgroud)
但我明白了
error C2801: 'mynamespace::operator bool' must be a non-static member
Run Code Online (Sandbox Code Playgroud)
来自我的视觉工作室.
但是,当我查找C2801解释时,它没有说明转换运算符(仅约=, - >,[],())
那么,有可能以某种方式写"将std :: bitset转换为bool运算符吗?"
(我不能b.any()在我的if语句中调用,因为当std :: bitset被unsigned或者某些东西替换时,必须运行相同的代码
typedef std::bitset<x> Bitset;
//typedef unsigned Bitset;
Run Code Online (Sandbox Code Playgroud)
所以理想的语法就像:
Bitset b = whatewer;
if(b)
doStuff();
Run Code Online (Sandbox Code Playgroud)
)
如果无法进行此重载,建议的解决方法是什么?
到目前为止,我使用它像:
if(b == Bitset(0))
doStuff();
Run Code Online (Sandbox Code Playgroud)
但我不喜欢它.
谢谢