0

Previously, I asked a question about evaluating a function receiving a std::pair at compile-time:

Why can I evaluate a function receiving a std::pair at compile-time, but not assert it?

And it seems as that's something that isn't possible with C++17, but it will be possible with C++20. Now, I'm looking if there's a way to mimic passing a std::pair to the function? Ideally, I wouldn't use a parameter pack, because I want to make it clear to the user that the values come in pairs.

Timo
  • 9,269
  • 2
  • 28
  • 58
Maki
  • 177
  • 7

1 Answers1

1

Yes it is possible. Simply create a compile time pair type:

template <auto First, auto Second>
struct pair
{
    static constexpr auto first = First;
    static constexpr auto second = Second;
};

Our tuple creation suddenly becomes cleaner aswell:

template<typename... Pairs>
constexpr auto foo() noexcept
{
    static_assert(((Pairs::second - Pairs::first >= 0) && ...));
    return std::tuple((Pairs::second - Pairs::first)...);
}

Here is a full example.

Timo
  • 9,269
  • 2
  • 28
  • 58
  • 1
    Why not ``? The question is tagged C++17, after all. – Nicol Bolas Feb 23 '20 at 15:48
  • @NicolBolas actually because I forgot that we can do this. Although the behavior is slightly different since both parameters can have different types then. – Timo Feb 23 '20 at 15:50
  • And integer promotion rules can also cause a headache if an unsigned value is passed by accident. – Timo Feb 23 '20 at 15:56