c++ - What is the meaning of "... ..." token? i.e. double ellipsis operator on parameter pack -
while browsing through gcc's current implementation of new c++11 headers, stumbled upon "......" token. can check, following code compiles fine [via ideone.com].
template <typename t> struct x { /* ... */ }; template <typename t, typename ... u> struct x<t(u......)> // line important 1 { /* ... */ }; so, meaning of token?
edit: looks trimmed "......" in question title "...", did mean "......" . :)
every instance of oddity paired case of regular single ellipsis.
template<typename _res, typename... _argtypes> struct _weak_result_type_impl<_res(_argtypes...)> { typedef _res result_type; }; template<typename _res, typename... _argtypes> struct _weak_result_type_impl<_res(_argtypes......)> { typedef _res result_type; }; template<typename _res, typename... _argtypes> struct _weak_result_type_impl<_res(_argtypes...) const> { typedef _res result_type; }; template<typename _res, typename... _argtypes> struct _weak_result_type_impl<_res(_argtypes......) const> { typedef _res result_type; }; my guess double ellipsis similar in meaning _argtypes..., ..., i.e. variadic template expansion followed c-style varargs list.
here's test supporting theory… think have new winner worst pseudo-operator ever.
edit: appear conformant. §8.3.5/3 describes 1 way form parameter list as
parameter-declaration-listopt ...opt
so double-ellipsis formed parameter-declaration-list ending parameter pack, followed ellipsis.
the comma purely optional; §8.3.5/4 say
where syntactically correct , “...” not part of abstract-declarator, “, ...” synonymous “...”.
this is within abstract-declarator, [edit] johannes makes point referring abstract-declarator within parameter-declaration. wonder why didn't "part of parameter-declaration," , why sentence isn't informative note…
furthermore, va_begin() in <cstdarg> requires parameter before varargs list, prototype f(...) allowed c++ useless. cross-referencing c99, illegal in plain c. so, bizarre.
usage note
by request, here demonstration of double ellipsis:
#include <cstdio> #include <string> template< typename t > t const &printf_helper( t const &x ) { return x; } char const *printf_helper( std::string const &x ) { return x.c_str(); } template< typename ... req, typename ... given > int wrap_printf( int (*fn)( req... ... ), given ... args ) { return fn( printf_helper( args ) ... ); } int main() { wrap_printf( &std::printf, "hello %s\n", std::string( "world!" ) ); wrap_printf( &std::fprintf, stderr, std::string( "error %d" ), 5 ); }
Comments
Post a Comment