c++c++17language-lawyerstdtuplestdapply

Should std::apply only apply to std::tuple?


The function signature of std::apply does not constrain the template parameter Tuple to be a specialization of std::tuple, so it can still accept a tuple-like objects that defines std::tuple_size_v (godbolt):

#include <tuple>
#include <utility>
#include <array>

int main() {
  std::apply([](int, int) {}, std::array{0, 0});
  std::apply([](int, int) {}, std::pair {0, 0});
  std::apply([](int, int) {}, std::tuple{0, 0});
}

But the description of std::apply in [tuple.apply] is:

20.5.5 Calling a function with a tuple of arguments

Does this mean that applying std::apply to objects other than std::tuple is undefined behavior?


Solution

  • 20.5.5 Calling a function with a tuple of arguments

    I highly doubt that the section titles are normative.

    The actual function is described as being equivalent to the reference implementation, which uses get and tuple_size_v to inspect the "tuple" parameter.

    Cppreference concurs.