简体   繁体   中英

No diagnostic for pack expansion in std::index_sequence

So I stumbled on this piece of code and I don't understand why the following construct is not ill-formed.

template<typename T, std::size_t... lst> 
struct mystruct : std::index_sequence<lst..., sizeof...(lst)> { 
  T i; 
};

int main() {
  mystruct<int> obj;
}

This should be ill-formed since the instantiation of mystruct with T = int will yield the following class(after T is substituted with int):

template<int, std::size_t... lst> 
struct mystruct : std::index_sequence<, //Empty list expansion
                                      0> { 
  int i; 
}; 

How is the std::index_sequence<, 0> not ill-formed? This above compiles without an error.

An empty parameter pack is never "empty" in a way that would make a construct syntactically invalid.

[temp.variadic]

7 When N is zero, the instantiation of the expansion produces an empty list. Such an instantiation does not alter the syntactic interpretation of the enclosing construct, even in cases where omitting the list entirely would otherwise be ill-formed or would result in an ambiguity in the grammar. [ Example:

 template<class... T> struct X : T... { }; template<class... T> void f(T... values) { X<T...> x(values...); } template void f<>(); // OK: X<> has no base classes // x is a variable of type X<> that is value-initialized

— end example ]

So it's not std::index_sequence<, 0> , but rather std::index_sequence<0> .

It is not illformed since it expands to std::index_sequence<0> . The parameter pack expansion works not just on a textual level but it also works with the intent of what is done. Otherwise, working with potentially empty parameter packs would be a real mess.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM