Home > Back-end >  how to move operator double to cpp file
how to move operator double to cpp file

Time:01-28

I want to move the operator double() function below to cpp, but I don't know how to write it correctly. Thank you.

template <typename T> class Max {
public:
  Max() = default;
  operator T() const { return Max<T>(); }
};

template <> class Max<double> {
public:
  Max() = default;
  operator double() const // how to move this implementation to cpp?
  {
    return MAX_REAL; // defined else where
  }
};

I want to move the operator double function to a cpp and keep the declaration in the header file like below. But it doesn't seem right.

Min.h

template <typename T> class Max {
public:
  Max() = default;
  operator T() const { return Max<T>(); }
};

template <> class Max<double> {
public:
  Max() = default;
  operator double() const;
};

Max.cpp

#include "Min.h"
template <> Max<double>::operator double() {
  return MAX_REAL;
}

CodePudding user response:

You are missing const in your definition, and template<> has to be removed.

So instead:

#include "Min.h"
template <> Max<double>::operator double() { 
  return MAX_REAL;
}

it has to be:

#include "Min.h"
Max<double>::operator double() const {
  return MAX_REAL;
}

CodePudding user response:

after some trial and error, below seems to compile alright.

#include "Min.h"
Max<double>::operator double() {
  return MAX_REAL;
}
  •  Tags:  
  • Related