0

While answering a different question, I could not find a reason for why the following code would be disallowed.

template <typename F> void bar (F *f) { f->a = 0; }

int main () {
    struct Foo { int a; } f = { 3 };
    bar(&f);                         // fail
}

The explanation I provided was that the compiler would have no way to legally express the template expansion, but that is more my intuition than a reason. Is there a definitive explanation as to why the code should not compile?

Community
  • 1
  • 1
jxh
  • 69,070
  • 8
  • 110
  • 193
  • @DavidRodríguez-dribeas: The problem was I was looking at the C++11 draft standard for a reason this was disallowed, and couldn't find it. However, I was using the compiler in it's default mode, not C++11 mode. The code does compile when I pass the C++11 switch to the compiler. – jxh Sep 19 '12 at 22:24

1 Answers1

6

That is explicitly disallowed in C++03, but legal code in C++11.

Not sure if this is an exact duplicate of Using local classes with STL algorithms, as the standard has changed since I asked that question.

Community
  • 1
  • 1
David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489