简体   繁体   中英

How to check a value and limits at compile time

I would like to create a settings class where a list of ID's has a default value and a limit to the value. All using constexpr to allow for compile time checks.

At compile time I would also like to validate the default values vs. the limits to ensure no illegal value is set. Here I hit a wall.

So, my base information is the following:

using item = std::variant<bool, int64_t, double, std::string_view>;

enum class ID {
    fs,
    fc,
    fosc
};


struct ItemLimit
{
    constexpr ItemLimit( item low, item high ) :
        Low( low ),
        High( high ){}

    const item Low;
    const item High;
};

struct item_entry{
    ID id;
    item default_value;
    ItemLimit limit;
};

I would like to be able to write the list the following way:

constexpr item_entry item_list[] = {
    {ID::fs,    12.0,               Limit( -12.0, 32.0 )},
    {ID::fc,    1244,               Limit( 4, 12333 )},
    {ID::fc,    false},
    {ID::fc,    5'000'000'000,      Limit( 1, 9999999999999999 )},
    {ID::fosc,  "HELLOOOOO"}
};

This requires a set of constructors, where I will limit to integer items for the following discussion.

Both Limit and item_entry now looks like this:

template <typename T>
struct ValueLimit
{
    constexpr ValueLimit( T low, T high ) :
        low( low ),
        high( high )
    {
    };

    const T low;
    const T high;
};

constexpr ValueLimit<int64_t> Limit( long long x, long long y ){
    return ValueLimit<int64_t>( x, y );
}


struct item_entry{
    constexpr item_entry( ID id, long long value, ValueLimit<int64_t> limit ) :
        id( id ),
        default_value( int64_t( value ) ),
        limit( limit.low, limit.high )
    {}


    ID id;
    item default_value;
};

Inside the item_entry constructor I would like to do a check for whether the value is inside the limits, but I can't figure out how. All my efforts end up in expressions that "are not evaluated to a constant".

The solution should ideally also work for floating point values.

Thanks in advance!

Henrik Andresen

The problem is that value is not a constant expression in this context, as function arguments are never constant expressions :

constexpr item_entry( ID id, long long value, ValueLimit<int64_t> limit ) 
                             ^~~~~~~~~~~~~~~

You need to pass value in such a way that it can be used as part of a constant expression : std::integral_constant is exactly what you need.

template <long long X> // <==
constexpr item_entry( ID id, 
                      std::integral_constant<long long,X> value, // <==
                      ValueLimit<int64_t> limit ) 
{
    static_assert(value >= limit.low && value <= limit.high); // <==
}

The same principle applies to limit :

template <typename T, T Low, T High>
struct ValueLimit
{
    static constexpr T low = Low;
    static constexpr T high = High;
};

Final changes:

struct item_entry
{
    template <long long X, typename Limit>
    constexpr item_entry( ID id, std::integral_constant<long long, X> value, Limit ) :
        id( id ),
        default_value( int64_t( value ) )
    {
        static_assert(value >= Limit::low && value <= Limit::high);        
    }

    ID id;
    item default_value;
};

Usage example:

template <long long X>
constexpr std::integral_constant<long long, X> ic{};

template <int64_t Low, int64_t High>
constexpr ValueLimit<int64_t, Low, High> limit{};

constexpr item_entry item_list[] = {
    {ID::fc,    ic<1244>,               limit< 4, 12333 >},
    {ID::fc,    ic<5'000'000'000>,      limit< 1, 9999999999999999 >}
};

live example on wandbox.org

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM