Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

safe_base only works for scalar types #44

Closed
akrzemi1 opened this issue Mar 15, 2017 · 2 comments
Closed

safe_base only works for scalar types #44

akrzemi1 opened this issue Mar 15, 2017 · 2 comments

Comments

@akrzemi1
Copy link
Member

Class template safe_base is defined as follows:

template<
    class Stored,
    Stored Min, // <-- non-type parameter
    Stored Max, // <-- non-type parameter
    class P,
    class E 
>
class safe_base;

Note that two values of type Stored are passed as template non-type parameters. This means that Stored can only be a (built-in) scalar type. Because only scalar types (apart from pointers and references) are allowed as non-type template parameters.

From reading the documentation one gets the impression that the library would work for any type that models Integer<T> concept, but that is not true. Even safe<safe<int>> doesn't work.

Technically, this could be fixed by using a different implementation of safe_base:

template<
    class Stored,
    class MinMaxPolicy, // <-- type parameter
    class P,
    class E 
>
class safe_base
{
    constexpr Stored Min = MinMaxPolicy::min();
    constexpr Stored Max = MinMaxPolicy::max();
    // ...
};
@robertramey
Copy link
Member

Of course you're correct on all this. For now though I'm not going to address it. But I have added a long section in Pending issues section discussing it. For what it's worth, my preference would be to do something like:

template<typename T>
struct range {
    T m_lowest;
    T m_highest;
    // default implementation
    range(
        const & T t_min, 
        const & T t_max
    ) :
        m_lowest(std::numeric_limits<T>::lowest(t_min),
        m_highest(std::numeric_limits<T>::max(t_max)
    {}
};

Then redeclare safe_base, etc. accordingly

@akrzemi1
Copy link
Member Author

One thing to add. In C++20 we will be able to use other literal types as template non-type parameters, not only scalars:

template< std::pair<int, long> P > // will be valid in C++20
struct X; 

It will not address all the types, but a wide variety.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants