What is the difference of friend iterator and friend class iterator which encounter in thinking in c++?
Asked Answered
S

1

7

In Thinking in C++ Volume 1, chapter 16: Introduction to Templates. The context:

Notice that instead of just saying:

friend iterator; // Make it a friend 

This code has:

friend class iterator; // Make it a friend

This is important because the name "iterator" is already in scope, from an included file.

What does Eckel really mean above? It seems friend iterator compiles correctly and I can't see the differences. Can anyone tell the answer? Thanks

Sussna answered 1/6, 2011 at 3:27 Comment(0)
C
6

As per C++03 standard section 11.4:

An elaborated-type-specifier shall be used in a friend declaration for a class.

So as per the specification the compiler will warn you that the friend declaration of iterator must be an elaborated class name. If not then the complier is non-compliant to the standard in this particular aspect.

What are Elaborated Type Specifiers?
C++ use elaborated type specifiers to tell the compiler explicitly to treat a class as a class. I think MSDN can explain it much better than I can, So check this out for detailed explanation.

Charteris answered 1/6, 2011 at 3:43 Comment(3)
Maybe an explanation on what elaborated type specifier means, because y'know, standardese isn't for everyone. :)Diathermic
@Xeo: Added a link to MSDN, I hope that shall suffice.:)Charteris
Also, in C++0x this restriction is lifted for template parameters, so we can now have template<class D> class Final{ friend D; ~Final(){} };.Diathermic

© 2022 - 2024 — McMap. All rights reserved.