4

Starting from C++20 one can use auto template argument to implement integral constant:

Try it online!

template <auto Value>
struct integral_constant2
    : std::integral_constant<decltype(Value), Value> {};

which can be used instead of more verbose variant std::integral_constant that has two template arguments.

Sure its easier to write f(std::integral_constant2<123>{}); instead of more verbose f(std::integral_constant<int, 123>{});. More than that you may not know type in advance if you have complex compile-time expression.

My question is whether there exists in C++20 std library anything like integral_constant2 mentioned above, not to reinvent wheel? Or at least some std constexpr function std::make_integral_constant(123) that deduces std::integral_constant's template params?

Enlico
  • 23,259
  • 6
  • 48
  • 102
Arty
  • 14,883
  • 6
  • 36
  • 69
  • 1
    fwiw, one could take this one step further and use a template trait that forwards the `auto` parameter to any template taking `typename T, T value`, though I am not aware of any higher order template in the standard lib – 463035818_is_not_an_ai Jun 10 '21 at 09:13

2 Answers2

3

No, I am not aware of such replacement.

I believe it would be difficult to defend such proposal, given how easy it is to write your own. On the other hand the only reason might be that nobody proposed it yet.


Mainly as a curiosity, and expanding on a comment, you can take this one step further via:

#include <type_traits>

template <auto Value, template<typename A, A> typename C>
using automized = C< decltype(Value),Value>;

template <auto Value>
using integral_constant = automized<Value,std::integral_constant>;

int main() {
    struct S {};
    integral_constant<true> c0{};
    integral_constant<10> c1{};
    integral_constant<S{}> c2{};
}

automized would allow to forward an auto parameter to any template taking typename T, T value. However, it is rather limited because it only works for templates taking exactly those parameters, while getting the general case right is rather painful when type and non-type parameters can be mixed.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • Also just updated very end of my question, do you know maybe at least there exists in std-lib some `constexpr` function like `std::make_integral_constant(123)` that deduces integral constant's template arguments and returns inantiated object? – Arty Jun 10 '21 at 09:27
  • @Arty its always difficult to ensure the non-existance of something. Afaik, no. Interesting that with `auto` template parameters `make_xyz` functions again turn out to be useful – 463035818_is_not_an_ai Jun 10 '21 at 09:30
  • I thought that std lib for any language should include ALL possible very-very common and generic patterns, even if they can be implemented in one line. So that when you look at your code you may see that most of code is implemented with std-only generics, seems like a nice thing especially if many other people read your code, using std-mostly generics will help to understand your code for more people. – Arty Jun 10 '21 at 09:35
  • @Arty I agree, though what gets into the std lib and what not, is largely a mystery to me, I am not into that. Also my `automized` is really just a curiosity, because it is of rather limited use. I'd rather expect to get something like you propose – 463035818_is_not_an_ai Jun 10 '21 at 09:40
  • @Arty you have to balance having all the possible bells and whistles with the committee's time to specify *correctly* all those bells and whistles – Caleth Jun 10 '21 at 09:42
2

You can see all new feature in C++20 here : https://en.cppreference.com/w/cpp/20

and I don't see anything related to integral_constant (and I don't see anything in the type_traits page either)

Martin Morterol
  • 2,560
  • 1
  • 10
  • 15