I’m implementing a class template that models a quantity as follows:
<code>template<typename T>
class Quantity{
private:
T m_val{};
public:
// A simple getter
T value() const{ return m_val; }
//Quantity Constructor:
template<typename R>
Quantity(const Quantity<R>& otherQty){
m_val = otherQty.value();
}
};
</code>
<code>template<typename T>
class Quantity{
private:
T m_val{};
public:
// A simple getter
T value() const{ return m_val; }
//Quantity Constructor:
template<typename R>
Quantity(const Quantity<R>& otherQty){
m_val = otherQty.value();
}
};
</code>
template<typename T>
class Quantity{
private:
T m_val{};
public:
// A simple getter
T value() const{ return m_val; }
//Quantity Constructor:
template<typename R>
Quantity(const Quantity<R>& otherQty){
m_val = otherQty.value();
}
};
Obviously in the constructor I’m doing a conversion from type ‘R’ to type ‘T’. I want to add a static_assert there to check that R is convertible to T without causing a narrowing conversion. Is there already a metafunction/type_trait to do so? If not, how can I implement my own?