joboccara / NamedType

Implementation of strong types in C++
MIT License
773 stars 85 forks source link

Type is template #8

Closed Trree closed 6 years ago

Trree commented 6 years ago

when I using

template<class T> using Width = NamedType<T, struct WidthTag>;
template<class T> using Height = NamedType<T, struct HeightTag>;
template<class T>
class Rectangle
{
public:
    Rectangle(Width<double> width, Height<double> height) : width_(width.get()), height_(height.get()) {}
    ......
}

so in the main.cpp, I have to write this:

Rectangle<double>  r(Width<double>(10), Height<double>(12));

when I put it into class:


template<class T>
class Rectangle
{
public:
    using Width = NamedType<T, struct WidthTag>;
    using Height = NamedType<T, struct HeightTag>;
    Rectangle(Width width, Height height) : width_(width.get()), height_(height.get()) {}
    ......
}

so in the main.cpp, I have to write this:

Rectangle<double>  r(Rectangle<double>::Width(10), Rectangle<double>::Height<double>(12));

I have a better choice or make it better?

Davidbrcz commented 6 years ago

Befora C++17; use a free function to have automatic type deduction In C++17: There is template type deduction for classes