【发布时间】:2020-07-02 06:34:50
【问题描述】:
在下面的代码中,有什么方法可以强制编译器使用隐式强制转换为 bool(在 b3 = b2 赋值中)而不是生成复制赋值运算符而不使用强制转换运算符?不幸的是,在显式删除复制分配代码后没有构建。缺乏显式转换对我的框架很重要。
class Base
{
public :
Base& operator=(const Base&) = delete;
virtual Base& operator=(bool state) { state_ = state; }
virtual operator bool() const { return state_; }
virtual bool operator!() const { return !state_; }
protected :
bool state_;
};
int main(void)
{
Base b1, b2, b3;
b1 = true;
b2 = !b1;
b3 = b2;
return 0;
}
更新: 错误是
test.cpp: In function ‘int main()’:
test.cpp:20:9: error: use of deleted function ‘Base& Base::operator=(const Base&)’
b3 = b2;
^~
test.cpp:6:10: note: declared here
Base& operator=(const Base&) = delete;
^~~~~~~~
更新2: 正如@serge-ballesta 所说的基本赋值运算符就足够了
#include <iostream>
class Base
{
public :
virtual Base& operator=(const Base &rhs) { std::cout << "BASE = BASE" << std::endl; return *this = static_cast<bool>(rhs); };
virtual Base& operator=(bool state) { std::cout << "BASE = bool" << std::endl; state_ = state; return *this; }
virtual operator bool() const { return state_; }
virtual bool operator!() const { return !state_; }
protected :
bool state_;
};
class Derived :
public Base
{
public :
virtual Base& operator=(bool state) { std::cout << "DERIVED = bool" << std::endl; state_ = state; /* And something more */ return *this; }
};
int main(void)
{
Base b1, b2, b3;
b1 = true;
b2 = !b1;
b3 = b2;
Derived d1, d2, d3, d4;
d1 = true;
d2 = !d1;
d3 = d2;
d4 = b3;
return 0;
}
输出是:
BASE = bool # b1 = true;
BASE = bool # b2 = !b1;
BASE = BASE # b3 = b2;
BASE = bool # ditto
DERIVED = bool # d1 = true;
DERIVED = bool # d2 = !d1;
BASE = BASE # d3 = d2;
DERIVED = bool # ditto
DERIVED = bool # d4 = b3;
有趣的是,在最后一种情况下,隐式转换是按照我的意愿完成的。
【问题讨论】:
-
我不确定编译器是否会在没有显式转换的情况下尝试执行此操作...可能会通过多种不同类型进行任意数量的此类转换。
-
如果你显式删除函数,那么不行,你不能这样做,错误会一直存在。
-
根据定义,您不能强制编译器使用隐式强制转换。强制转换始终是显式的:它是您在源代码中编写的内容,用于告诉编译器进行转换。您要查找的术语是隐式转换。
-
@PeteBecker 是的,这句话很不幸 - 我的意思是使用隐式转换来指导编译器 -> 带有转换值的运算符