首页 > 其他分享 >conversion-operator

conversion-operator

时间:2024-04-17 15:47:15浏览次数:26  
标签:conversion const struct bool constructor operator

参考文档

user-defined conversion function - cppreference.com
The Safe Bool Idiom - 知乎

一般形式为operator *type*() const, 比如:
operator int() const;
operator bool() const;
operator AA() const;

自定义类型转换

struct To
{
    To() = default;
    To(const struct From&) {} // converting constructor
};

struct From
{
    operator To() const {return To();} // conversion function
};

int main()
{
    From f;
    To t1(f);  // direct-initialization: calls the constructor
    // Note: if converting constructor is not available, implicit copy constructor
    // will be selected, and conversion function will be called to prepare its argument

//  To t2 = f; // copy-initialization: ambiguous
    // Note: if conversion function is from a non-const type, e.g.
    // From::operator To();, it will be selected instead of the ctor in this case

    To t3 = static_cast<To>(f); // direct-initialization: calls the constructor
    const To& r = f;            // reference-initialization: ambiguous
}

为什么operator bool()需要用explicit修饰?

c++ - Why does declaring an operator bool() const member overload the [] operator? - Stack Overflow

The operator is coming from the built-in subscript operator which treats expressions A[B] as *(A + B).
This results in the evaluation of *(1 + "wut") => 'u', which then causes the if condition to pass, as 'u' is a non-zero value.
Declare your member as explicit operator bool() to prevent your type from being implicitly converted to other integral types.

#include <iostream>
using namespace std;

struct Test {
    operator bool() const {
        return true;
    }
};

int main(int argc, char** argv) {
    Test test;

    if (test["wut"])
        cout << "Success (test[\"wut\"])\n";
}

一个operator bool()的坑

c++ - Why is my "explicit operator bool()" not called? - Stack Overflow

标签:conversion,const,struct,bool,constructor,operator
From: https://www.cnblogs.com/devin1024/p/18140882

相关文章

  • 3.0 常见operators算子
    1.1卷积相关1)卷积2)反卷积(只能做到近似恢复,无法完全恢复原图像) 参考:https://blog.csdn.net/qq_27261889/article/details/863040611.2线性变换相关1)Linear2)矩阵相乘类:【mm:二维矩阵相乘;bmm:三维矩阵相乘;matmul:多维矩阵相乘,只要两个矩阵能够broadcast即......
  • 30 天精通 RxJS (20):Observable Operators - window, windowToggle, groupBy
    前几天我们讲完了能把HigherOrderObservable转成一般的Observable的operators,今天我们要讲能够把一般的Observable转成HigherOrderObservable的operators。其实前端不太有机会用到这类型的Operators,都是在比较特殊的需求下才会看到,但还是会有遇到的时候。Op......
  • 30 天精通 RxJS (17):Observable Operators - switch, mergeAll, concatAll
    今天我们要讲三个operators,这三个operators都是用来处理HigherOrderObservable。所谓的HigherOrderObservable就是指一个Observable送出的元素还是一个Observable,就像是二维数组一样,一个数组中的每个元素都是数组。如果用泛型来表达就像是Observable<Observab......
  • 30 天精通 RxJS (16):Observable Operators - catch, retry, retryWhen, repeat
    我们已经快把所有基本的转换(Transformation)、过滤(Filter)和合并(Combination)的operators讲完了。今天要讲错误处理(ErrorHandling)的operators,错误处理是异步行为中的一大难题,尤其有多个交错的异步行为时,更容易凸显错误处理的困难。就让我们一起来看看在RxJS中能如何处理......
  • 30 天精通 RxJS (14):Observable Operator - throttle, debounce
    昨天讲到了在UI操作上很常用的delay,今天我们接着要来讲另外两个也非常实用operators,尤其在做性能优化时更是不可或缺的好工具!Operatorsdebounce跟buffer、bufferTime一样,Rx有debounce跟debounceTime一个是传入observable另一个则是传入毫秒,比较常用到的是de......
  • 问题解决 usr/include/c++/11/bits/list.tcc:344:24: error: no match for ‘operator
    1.问题解决usr/include/c++/11/bits/list.tcc:344:24:error:nomatchfor‘operator==’错误解释:这个编译错误表明编译器在尝试使用==操作符比较两个对象时找不到匹配的操作符函数。在C++中,如果你尝试比较两个自定义类型的对象,且没有为这些对象定义==操作符,编译器将无法进......
  • 30 天精通 RxJS (09):Observable Operator - skip, takeLast, last, concat, startWith, merge
    运营商skip我们昨天介绍了take可以取前几个送出的元素,今天介绍可以略过前几个送出元素的operator:skip,范例如下:varsource=Rx.Observable.interval(1000)varexample=source.skip(3)example.subscribe({ next:(value)=>{ console.log(value) }, error:(err)......
  • C++:重载符operator
    我们可以在类中使用重载符进行符号功能重载<返回类型>operator<重载的符号>(形参){//function} 下面重载一个符号"≥"classMyClass{public:inta;charb;public:MyClass();~MyClass();intoperator>=(MyClassA){......
  • IfcConversionBasedUnit
    IfcConversionBasedUnit实体定义IfcConversionBasedUnit用于定义具有基本单位转换率的单位。为了识别一些常用的基于转换的单位,表4中列出了Name属性的标准名称(不区分大小写)。 NameDescription'inch'Lengthmeasureequalto25.4mm'foot'Lengthmeasureequalto30......
  • 操作符重载(operator overloading)
    操作符重载(operatoroverloading)成员函数所有的成员函数一定带着一个隐藏的参数示例代码:inlinecomplex&complex::operator+=(constcomplex&r){//调用者就是this,这是一个指针.+=符号作用在左边的数,左边的数就是this.->编译器自动会传入c2的指针//this......