0

I have a simple primitive type wrapper:

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;
};

Casting Scalar values works well, but somehow it fails for references, e.g.

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

Here's the compilation error (can be checked here http://rextester.com/GOPYU13091), any ideas what might be the issue here?

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);
                                               ^
Rakete1111
  • 47,013
  • 16
  • 123
  • 162
misev
  • 349
  • 3
  • 11

1 Answers1

5

This is a const vs non-const issue, not a reference vs object issue.

Using

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

works for me.

By changing the user defined conversion operator to const member function

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

also makes sure that the following works.

const auto& c = a;
Scalar<int> d = static_cast<Scalar<int>>(c);
R Sahu
  • 204,454
  • 14
  • 159
  • 270