繁体   English   中英

如何在编译时检查值和限制

[英]How to check a value and limits at compile time

我想创建一个设置类,其中 ID 列表具有默认值和值限制。 全部使用 constexpr 来允许编译时检查。

在编译时,我还想验证默认值与限制以确保没有设置非法值。 我在这里撞墙了。

所以,我的基本信息如下:

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;
};

我希望能够按以下方式编写列表:

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"}
};

这需要一组构造函数,我将在下面的讨论中限制为整数项。

Limit 和 item_entry 现在看起来像这样:

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;
};

在 item_entry 构造函数中,我想检查该值是否在限制范围内,但我不知道如何进行。 我所有的努力都以“未评估为常数”的表达式结束。

理想情况下,该解决方案也适用于浮点值。

提前致谢!

亨利克·安德森

问题是value在这种情况下不是常量表达式,因为函数参数永远不是常量表达式

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

您需要以一种可以将其用作常量表达式的一部分的方式传递valuestd::integral_constant正是您所需要的。

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); // <==
}

同样的原则适用于limit

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

最终变化:

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;
};

用法示例:

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 >}
};

wandbox.org 上的现场示例

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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