草庐IT

OPERATOR

全部标签

c++ - `pair::operator=(pair&&)` 错误与 `auto&` 推断 move 操作 - libstdc++ 回归?

鉴于这个程序:structVal{Val()=default;Val(Val&&)=default;auto&operator=(Val&&);};/*PLACEHOLDER*/auto&Val::operator=(Val&&){return*this;}替换/*PLACEHOLDER*/与...intmain(){std::vector>v;v.emplace(std::begin(v),0,Val{});}...编译成功:g++6.2.0g++6.3.0g++7.0.1(主干)clang++3.9.1clang++5.0.0(HEAD)onwandbox替换/*PLACEHOLD

c++ - 三元运算符的奇怪隐式转换

我有以下代码:classA{public:operatorint()const{return5;}};classB{public:operatorint()const{return6;}};intmain(){Aa;Bb;intmyInt=true?a:b;return0;}尝试使用VisualStudio2017RC编译该代码会导致以下错误:errorC2446:::noconversionfromBtoAnote:Nouser-defined-conversionoperatoravailablethatcanperformthisconversion,ortheoperatorc

c++ - 三元运算符的奇怪隐式转换

我有以下代码:classA{public:operatorint()const{return5;}};classB{public:operatorint()const{return6;}};intmain(){Aa;Bb;intmyInt=true?a:b;return0;}尝试使用VisualStudio2017RC编译该代码会导致以下错误:errorC2446:::noconversionfromBtoAnote:Nouser-defined-conversionoperatoravailablethatcanperformthisconversion,ortheoperatorc

c++ - 为什么字符串支持 `operator=(char)` ?

我的一位同事今天在我们的代码中发现了一个非常微妙的错误,基本上是这样的:doubled=65;std::strings="Helloworld";//..somewherelater,accidentallyassigningtosinsteadofasimilarly//namednumericalvariable.s=d;//sisnow'A'我发现这个错误发生的原因是std::basic_string有一个赋值运算符_Myt&operator=(_Elem_Ch){//assign1*_Chreturn(assign(1,_Ch));}现在编译器并没有真正提示(很多,如果级别足够

c++ - 为什么字符串支持 `operator=(char)` ?

我的一位同事今天在我们的代码中发现了一个非常微妙的错误,基本上是这样的:doubled=65;std::strings="Helloworld";//..somewherelater,accidentallyassigningtosinsteadofasimilarly//namednumericalvariable.s=d;//sisnow'A'我发现这个错误发生的原因是std::basic_string有一个赋值运算符_Myt&operator=(_Elem_Ch){//assign1*_Chreturn(assign(1,_Ch));}现在编译器并没有真正提示(很多,如果级别足够

c++ - 我可以模板化用户定义的文字吗?

假设我有一些类(class):templateclassFoo{constT*x_;public:Foo(constT*str):x_{str}{}};我提供了一些创建Foo对象的用户定义文字:Foooperator""_foo(constchar*str,std::size_t){returnFoo{str};}Foooperator""_foo(constwchar_t*str,std::size_t){returnFoo{str};}//etc.forchar16_tandchar32_t.我的问题是:为什么我不能将这些模板化而不必重写代码?templateFoooperator

c++ - 我可以模板化用户定义的文字吗?

假设我有一些类(class):templateclassFoo{constT*x_;public:Foo(constT*str):x_{str}{}};我提供了一些创建Foo对象的用户定义文字:Foooperator""_foo(constchar*str,std::size_t){returnFoo{str};}Foooperator""_foo(constwchar_t*str,std::size_t){returnFoo{str};}//etc.forchar16_tandchar32_t.我的问题是:为什么我不能将这些模板化而不必重写代码?templateFoooperator

c++ - 重载运算符 ->

这是我的代码示例:classX{public:voidf(){}};classY:publicX{public:X&operator->(){return*this;}voidf(){}};intmain(){Yt;t.operator->().f();//OKt->f();//errorC2819:type'X'doesnothaveanoverloadedmember'operator->'//errorC2232:'->Y::f':leftoperandhas'class'type,use'.'}为什么编译器试图将operator->的责任从Y转移到X?当我实现X::op->时,

c++ - 重载运算符 ->

这是我的代码示例:classX{public:voidf(){}};classY:publicX{public:X&operator->(){return*this;}voidf(){}};intmain(){Yt;t.operator->().f();//OKt->f();//errorC2819:type'X'doesnothaveanoverloadedmember'operator->'//errorC2232:'->Y::f':leftoperandhas'class'type,use'.'}为什么编译器试图将operator->的责任从Y转移到X?当我实现X::op->时,

c++ - 为什么这个涉及重载运算符和隐式转换的 C++ 表达式不明确?

operatorbool打破operator的使用在下面的例子中。谁能解释为什么bool在if(a中同样相关表达式作为具体的操作符,是否有变通方法?structFoo{Foo(){}Foo(intx){}operatorbool()const{returnfalse;}friendbooloperator当我编译时,我得到:g++foo.cppfoo.cpp:Infunction'intmain()':foo.cpp:18:11:error:ambiguousoverloadfor'operatorfoo.cpp:8:17:note:candidate:booloperator