0

Let's keep it short. Why does the following code compile successfully...

#define RUN_FORALL(pattern) (int[]){0, ((pattern), void(), 0)...}
template <typename ...Attributes>
class Foo : public Attributes... {
  template <typename T>
  void bar(T o) { RUN_FORALL(Attributes::value = o.getValue()); }
};
int main() {}

... and the following code does not?

#define RUN_FORALL(pattern) (int[]){0, ((pattern), void(), 0)...}
template <typename ...Attributes>
class Foo : public Attributes... {
  template <typename T>
  void bar(T o) { RUN_FORALL(Attributes::value = o.getValue<Attributes>()); }
};
int main() {}

Error:

1:30: error: expected primary-expression before 'int'
5:19: note: in expansion of macro 'RUN_FORALL'

How can I call a function template in a pack expansion pattern?

kdopen
  • 8,032
  • 7
  • 44
  • 52
user1494080
  • 2,064
  • 2
  • 17
  • 36
  • Do you think you could provide a [mcve]? I'd like to compile the top snippet myself. – AndyG Sep 07 '16 at 17:00
  • 1
    Missing `template` disambiguation, my friend. Also `(int[]) { .. }` is non-standard syntax borrowed from C. – Johannes Schaub - litb Sep 07 '16 at 17:01
  • @AndyG I am not sure what you are missing. You can take both snippets, copy them as they are to e.g. `www.cpp.sh` and observe that the above compiles successfully and the below does not. – user1494080 Sep 07 '16 at 17:07
  • `o.template getValue()`. This has nothing to do with pack expansions. – T.C. Sep 07 '16 at 17:11
  • @JohannesSchaub-litb Are you sure that this is non-standard syntax? Have a look at [link](http://en.cppreference.com/w/cpp/language/list_initialization): `T { arg1, arg2, ... };` initializes an unnamed temporary with a braced-init-list. – user1494080 Sep 07 '16 at 17:16
  • @user1494080 yes.. you can ask a new question if you are unsure about it. it is nonstandard and not shown on the site you link – Johannes Schaub - litb Sep 07 '16 at 19:42
  • `(int []) { stuff }` is a C99 compound literal. The `T` in C++ has to be a *simple-type-specifier* or a *typename-specifier*; `(int [])` is neither. – T.C. Sep 07 '16 at 20:52

0 Answers0