max66
max66

Reputation: 66210

zero length variadic expansion of ill-formed call

Question for standard gurus.

Trying to respond to another question, I came to doubt about the well-formedness of a code.

As far I know, the following code is ill-formed

int main ()
 {
   std::tuple<>  a;

   std::get<0>(a);
 }

because a call to std::get<I>(t), when t is a std::tuple<Ts...>, is ill-formed when I is outside the range [0, sizeof...(Ts)[.

In this case sizeof...(Ts) is zero, so the range [0, 0[ is empty, so std::get<I>(a) is ill-formed for every index I.

But when std::get<I>(a) is expanded through an empty variadic pack?

I mean: the following code

#include <tuple>

template <typename ... Args>
void bar (Args const & ...)
 { }

template <std::size_t ... I>
void foo ()
 {
   std::tuple<> a;

   bar( std::get<I>(a) ... );
 }

int main ()
 {
   foo<>();
 }

that uses a ill-formed (?) call (std::get<I>(a)) but zero-time variadic expanded (sizeof...(I) is zero), is well-formed or ill-formed?

Upvotes: 3

Views: 280

Answers (1)

T.C.
T.C.

Reputation: 137315

[temp.res]/8:

The program is ill-formed, no diagnostic required, if:

  • [...]
  • every valid specialization of a variadic template requires an empty template parameter pack, or
  • [...]

Upvotes: 6

Related Questions