Reputation: 51
I would like to run the first instantiation instead of the second, but the compiler takes the arguments as integers and returns errors.
Note that the use of optional is to allow the object to receive a boost::none (C++ version of python's None).
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/optional.hpp>
using namespace boost::multiprecision;
template<class T>
struct Point
{
explicit Point(T x, T y): x (x), y (y){}
T x;
T y;
};
typedef boost::optional<int128_t> oint128_t;
int main(){
// Point<oint128_t> P(90, 5); // this fails
Point<oint128_t> Q((oint128_t) 91, (oint128_t) 31); // this works
}
Upvotes: 0
Views: 43
Reputation: 70277
You can always put the explicit casts inside of the Point
constructor.
template<typename S>
explicit Point(S x, S y): x(T(x)), y(T(y)) {}
But I suspect that doing so will introduce more confusing type errors in the long run. My recommendation is to explicitly call the boost::optional
constructor like you're supposed to. T
is not meant to be an instance of boost::optional<T>
. The cast you're invoking is actually calling a converting constructor and making a new object; you're just making it look like a conversion is taking place.
Upvotes: 1