簡體   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