c++ - Variadic expander without a typedef -
one trick used perform variadic parameter expansion using un-sized array typedef in combination comma operator, below:
#include <iostream> template<typename... ts> void expander(ts&&... ts) { using expand = int[]; (void)expand{0, (std::forward<ts>(ts)(), 0)...}; } void f() { std::cout << __pretty_function__ << std::endl; } int main() { expander(f, f); }
can without introducing typedef? if try directly
(void)int[]{0, (std::forward<ts>(ts)(), 0)...};
gcc/clang spit out
error: expected primary-expression before 'int'
and if try parenthesize, code compiles believe non-standard compliant:
warning: iso c++ forbids compound-literals [-wpedantic]
in expression
(void)int[]{0, (std::forward<ts>(ts)(), 0)...};
you're trying use functional notation cast create temporary array. won't work because language allows functional notation used either simple-type-specifier or typename-specifier.
from [expr.type.conv]/3
similarly, simple-type-specifier or typename-specifier followed braced-init-list creates temporary object of specified type direct-list-initialized (8.5.4) specified braced-init-list, , value temporary object prvalue.
and if go lookup definition of simple-type-specifier under [dcl.type.simple], doesn't include array brackets. in fact, doesn't include that's more 1 word. why writing int(1)
valid, signed int(1)
isn't.
so, c++11/14 need typedef
or declare array variable. but, c++1z compiler, can make use of fold expressions , avoid both
template<typename... ts> void expander(ts&&... ts) { (void(std::forward<ts>(ts)()), ...); }
Comments
Post a Comment