相关疑难解决方法(0)

了解涉及用户定义转换的重载决策排名

我试图了解重载决议.

首先让我们考虑第一种情况:

struct int1{
   int val;
   operator int&()&{
      return val;
      }
   operator const int &() const&{
      return val;
      }
    };

void f(int &){}       //f#1
void f(const int&){}  //f#2

void test1(){
  int1 x;
  f(x);
   //Conversion sequence for f#1: 
   //   - int_wrapper& --> int1::operator int&
   //   => Ranking: user defined conversion rank
   //Converison sequence for f#2:
   //   - int1& --> int1::operator int & --> const int&
   //   - int1& --> const int1 &         --> int1::operator const int& 
   //   => Ranking: …
Run Code Online (Sandbox Code Playgroud)

c++ overloading language-lawyer

6
推荐指数
1
解决办法
241
查看次数

标签 统计

c++ ×1

language-lawyer ×1

overloading ×1