A simple sample code involving templates, friends and lookup
Hello, I'm aware about these issues being around for quite a while now, but there is still a chance this sample code can be helpful. I apologize if someone gets annoyed :-P template struct Foo { template friend void func(const Foo &); }; void check(const Foo & x) { // Foo weird; // uncomment this line and all works func(x);// <-- ERROR } Tested with gcc 4.0 - 4.3, and all behave the same: "error: ‘func’ was not declared in this scope" but it works if you uncomment the weird line. Best regards, Dragan Milenkovic
Re: A simple sample code involving templates, friends and lookup
Richard Guenther wrote: [snip] template struct Foo { template friend void func(const Foo &); }; void check(const Foo & x) { // Foo weird; // uncomment this line and all works func(x);// <-- ERROR } Tested with gcc 4.0 - 4.3, and all behave the same: "error: 'func' was not declared in this scope" but it works if you uncomment the weird line. Actually even with the weird line the program is invalid. What are you trying to do? ;) Richard. Ok... afaik, that func should be defined on that very place where it is declared as friend. But could you please elaborate why it is invalid, since you made me start questioning my C++ knowledge... :-D Dragan
Re: A simple sample code involving templates, friends and lookup
Jonathan Wakely wrote: On 17/01/2008, Richard Guenther wrote: Well, a language lawyer can probably clear things up. From a look at the std it looks like w/o a previous declaration the above should be invalid. And at a different point it suggests the decl becomes available. Yes, at the point of instantiation of Foo the friend is declared, and can then be found by ADL because Foo is an associated type. The reference parameter 'x' doesn't cause an instantiation, only 'weird' does. However, see the discussion in section 9.2.2 of Vandevoorde and Josuttis' C++ Templates book and the footnote saying the standard isn't exactly clear. [snip] Thanks for clarification and info. I believe issue #34 addresses exactly what we're talking about. http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_closed.html#34 Hey... the current number of issues is kinda scary... :-O Dragan