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?

1
  • 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 Commented Jun 10, 2021 at 9:13

2 Answers 2

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.

5
  • 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
    Commented Jun 10, 2021 at 9: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 Commented Jun 10, 2021 at 9: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
    Commented Jun 10, 2021 at 9: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 Commented Jun 10, 2021 at 9: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
    Commented Jun 10, 2021 at 9: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)

1
  • Thanks for investigation of these pages, upvoting your answer!
    – Arty
    Commented Jun 10, 2021 at 9:59

Not the answer you're looking for? Browse other questions tagged or ask your own question.