C++ Logo


Advanced search

Re: std::size_t-specializations of std::integral_constant

From: Jason McKesson <jmckesson_at_[hidden]>
Date: Sat, 28 Aug 2021 15:18:56 -0400
On Sat, Aug 28, 2021 at 2:19 PM kang hewill via Std-Proposals
<std-proposals_at_[hidden]> wrote:
> Hi C++ expert,
> I have been wondering why we have std::index_sequence but not std::size_constant, that is:
> template<std::size_t N>
> using size_constant = std::integral_constant<std::size_t, N>;
> Since std::integral_constant<std::size_t, N> is widely used in the standard library, such as tuple_size and variant_size need to inherit it, so I think it is appropriate to add an alias to it in the standard, which can reduce a lot of typing and improve some readability, just like we aliased bool_constant<b> to integral_constant<bool, b>.

I wouldn't call it "widely used". It's only used in the two places
that you mentioned: things relating to `tuple_size` and
`variant_size`. Even its use in the exposition-only `pair-like`
concept is just about using `tuple_size`.

By contrast, `bool_constant` (and its attendant `true_type` and
`false_type` meta-values) get used quite frequently.

Though to be fair, writing a tuple-like type which can be used in
structured binding does require using that *specific* template, so it
wouldn't be off-base to make an alias for it.

Received on 2021-08-28 14:19:09