joboccara / NamedType

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

Add skill Dereferencable #52

Closed pemessier closed 3 years ago

pemessier commented 3 years ago

I use the following skill to add the dereference operator (like in std::optional for example):

template< typename T >
struct Dereferencable;`

template< typename T, typename Parameter, template< typename > class ... Skills >
struct Dereferencable< fluent::NamedType< T, Parameter, Skills... > > : fluent::crtp< fluent::NamedType< T, Parameter, Skills... >, Dereferencable >
{
    constexpr const T& operator*() const &
    {
        return this->underlying().get();
    }

    constexpr T& operator*() &
    {
        return this->underlying().get();
    }
};

Example usage:

void Fct( double value );
...
Fct( *strongType );

Note that I also added [[nodiscard]], but this could be a separate ticket

pemessier commented 3 years ago

Merged in #53