用户定义的转换运算符不适用于引用

User-defined conversion operator doesn't work on references

我有一个简单的原始类型包装器:

template <typename T>
class Scalar {
 public:
  explicit Scalar(T value) : value{value} {}

  Scalar(Scalar&& other) = default;
  Scalar& operator=(Scalar&& other) = default;
  Scalar(const Scalar& other) = default;
  Scalar& operator=(const Scalar& other) = default;

  template <typename U>
  explicit operator Scalar<U>() {
    return Scalar<U>{static_cast<U>(this->value)};
  }

  inline T getValue() const noexcept { return this->value; }
 private:
  T value;
};

转换标量值效果很好,但不知何故它无法引用,例如

auto a = Scalar<double>{2.54};
Scalar<int> b = static_cast<Scalar<int>>(a); // works

const auto& c = a;
Scalar<int> d = static_cast<Scalar<int>>(c); // fails

这是编译错误(可以在此处查看 http://rextester.com/GOPYU13091),您知道这里可能是什么问题吗?

source_file.cpp: In function ‘int main()’:
source_file.cpp:31:47: error: no matching function for call to ‘Scalar(const Scalar<double>&)’
     Scalar<int> d = static_cast<Scalar<int>>(c);
                                               ^
source_file.cpp:12:3: note: candidate: Scalar<T>::Scalar(const Scalar<T>&) [with T = int] <near match>
   Scalar(const Scalar& other) = default;
   ^
source_file.cpp:12:3: note:   conversion of argument 1 would be ill-formed:
source_file.cpp:31:47: error: could not convert ‘c’ from ‘const Scalar<double>’ to ‘const Scalar<int>&’
     Scalar<int> d = static_cast<Scalar<int>>(c);
                                               ^
source_file.cpp:10:3: note: candidate: Scalar<T>::Scalar(Scalar<T>&&) [with T = int] <near match>
   Scalar(Scalar&& other) = default;
   ^
source_file.cpp:10:3: note:   conversion of argument 1 would be ill-formed:
source_file.cpp:31:47: error: could not convert ‘c’ from ‘const Scalar<double>’ to ‘Scalar<int>&&’
     Scalar<int> d = static_cast<Scalar<int>>(c);
                                               ^

这是一个 const 与非 const 的问题,而不是引用与对象的问题。

正在使用

auto& c = a;
Scalar<int> d = static_cast<Scalar<int>>(c);

适合我。

通过将用户定义的转换运算符更改为 const 成员函数

template <typename U>
explicit operator Scalar<U>() const {
  return Scalar<U>{static_cast<U>(this->value)};
}

还确保以下工作正常。

const auto& c = a;
Scalar<int> d = static_cast<Scalar<int>>(c);