Consider the following short program.
#include <iostream>
template< typename ... Ts >
class foobar
{
static_assert( sizeof...(Ts) > 0 );
};
template< typename ... Ts >
std::ostream& operator<<( std::ostream& o, const foobar< Ts... >& )
{
return o;
}
int main(void)
{
std::cout << "This has nothing to do with foobar" << std::endl;
}
When we try to compile this, we get...
ted@tedscomputer:~/Projects/emptypack$ g++ -o emptypack main.cpp
main.cpp: In instantiation of ‘class foobar<>’:
main.cpp:17:63: required from here
main.cpp:6:34: error: static assertion failed
6 | static_assert( sizeof...(Ts) > 0 );
| ~~~~~~~~~~~~~~^~~
ted@tedscomputer:~/Projects/emptypack$
I understand that std::endl
is a bit of an odd bird (so much so that it actually has its own StackOverflow tag), but what exactly is going wrong here? Why is the compiler trying to instantiate my completely unrelated class with empty type parameters, failing, and then generating an error?
Maybe more to the point, every time I write a class that takes a parameter pack, do I have to make sure it can be instantiated with no type parameters, even if that doesn't make any sense w.r.t. the program logic, just in case it comes within a mile of std::endl
?
requires
fixes the issue. – HsiuhsuTs
as an empty parameter pack (https://mcmap.net/q/512196/-when-are-template-parameter-packs-deduced-as-empty/5754656) a substitution failure instead of hard failing when instantiating the class (to look for a converting constructor I think?) – Boydboyden