Initialize integer literal to std::size_t

前端 未结 3 1683
醉梦人生
醉梦人生 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:23

    Depending on the function, you may also be able to do this and may find it cleaner:

    auto result = func(1, some_var);
    

    For example, I've done this with std::max:

    auto result = std::max(0, std::min(index, vec.size()-1));
    

    By explicitly specifying the template instantiation, the casts can be implicit. However, do note that this is a cast, so is susceptible to errors which Potatoswatter's brace initialization isn't.

提交回复
热议问题