Initialize integer literal to std::size_t

前端 未结 3 1668
醉梦人生
醉梦人生 2021-01-03 18:18

There are known ways to manipulate the type of an integer literal

0L;  // long
3U;  // unsigned integer
1LL; // long long

What I need is a

3条回答
  •  清酒与你
    2021-01-03 18:32

    There is no dedicated suffix for std::size_t. In C++11, you could create a user-defined literal for it, though:

    std::size_t operator "" _sz (unsigned long long int x)
    {
      return x;
    }
    
    // Usage:
    
    auto s = 1024_sz;
    
    static_assert(std::is_same::value, "He's wrong");
    

    Live example

提交回复
热议问题