Home
Fractals
Tutorials
Books
Archive
My blog
My LinkedIn Profile

BOOKS i'm reading

Cryptography engineering, Niels Ferguson, Bruce Schneier, Tadayoshi Kohno, ISBN: 9780470474242
Advanced Programming in the UNIX(R) Environment (2nd Edition), W. Richard Stevens, Stephen A. Rago, ISBN:0201433079
Trading For a Living, Alexander Elder, ISBN:0471592242

mailto:olivier@olivierlanglois.net

friend function declaration in a class template

07/09/07

Permalink 10:21:28 pm, by lano1106 Email , 458 words, 1735 views   English (CA)
Categories: C++

friend function declaration in a class template

In my opinion, one of the most twisted part of the C++ standard (I am sure that there is more than just one twisted aspect) it is the friend function declaration in a class when you add templates to the mix. Even compiler implementers are having a hard time to get it right. I had the following code that used to compile fine in VC++6:

template< typename T >
class X
{
    friend ostream &operator<<( ostream &, const X<T> & );
};

template< typename T >
ostream &operator( ostream &os, const X<T> &t )
{
    // Print out t
    return os;
}

but now with VC++2003, I received this error message:

unresolved external symbol ostream &operator<<( ostream &, const X<T> & )

That code snippet is non conforming to the standard and in order to make the compiler happy, I had to write:

template< typename T >
class X
{
    /*
     * The '<>' is needed to say that the friend is a
     * function template.
     */
    friend ostream &operator<< <>( ostream &,
                                   const X<T> & );
};

and for those who wonder, no I did not find the solution by myself. I had to look into my book C++ Templates. From section 8.4.1 (Friend Functions):

"An instance of a function template can be made a friend by making sure the name of the friend function is followed by angle brackets...If the name is not followed by angle brackets, there are two possibilities:

1. If the name isn't qualified (in other words, it doesn't contain a double colon), it never refers to a template instance...
2. If the name is qualified (it contains ::), the name refer to a previously declared function or function template. A matching function is preferred over a matching function template..."

So my example fits case #1 description so because of a bug into VC++6 (or maybe the standard was even not complete at the time of its release...), the compiler was erroneously accepting the friend statement as referring to a function template.

So this is the whole story. What I find sad is that the rule is not intuitive at all. There is no way I can reason it next time I am in the same situation. I will either remember the rule or I will have to check back in my C++ Template book. Herb Sutter has also written an item on that topic in his book C++ exceptional Style if you are interested in a different point of view on the topic but he pretty much comes to the conclusion that this is a tricky C++ standard aspect and that a lot of compilers have troubles accepting such statements.

Comments, Pingbacks:

Comment from: lano1106 [Member] Email
I just found out that this topic is briefly covered in Bjarne Stroustrup book 'The C++ Programming language' in section C.13.2
(Section Templates, subsection Friends).

However, I found the coverage more complete in the C++ Templates.

PermalinkPermalink 09/15/07 @ 00:49
Comment from: FONTAINE Alain [Visitor] Email
Hello,
What is unclear to me is:
Why to you have to precise const in:
const X some_type & );
Otherwise, w/o Const, the gcc
compiler gave a lot of errors!!!

PermalinkPermalink 10/06/07 @ 01:52
Comment from: lano1106 [Member] Email
All const is specifying is that your operator<< will not modify its operand. It is the same as in:

x = y + z;

You would be surprised if the addition would modify y or z.

However, this is just a convention. It is not illegal to provide non const reference.

Your errors are probably because you are passing a right hand side value. If the parameter is a const reference, the compiler is allowed to create a temporary X some_type object. Otherwise, it will complain about it.

Please consult the book 'The C++ Programming language' section 5.5 for more details.
PermalinkPermalink 10/06/07 @ 02:05

This post has 14 feedbacks awaiting moderation...

Leave a comment:

Your email address will not be displayed on this site.
Your URL will be displayed.

Allowed XHTML tags: <p, ul, ol, li, dl, dt, dd, address, blockquote, ins, del, span, bdo, br, em, strong, dfn, code, samp, kdb, var, cite, abbr, acronym, q, sub, sup, tt, i, b, big, small>
(Line breaks become <br />)
(Set cookies for name, email and url)
(Allow users to contact you through a message form (your email will NOT be displayed.))

Olivier Langlois's blog

I want you to find in this blog informations about C++ programming that I had a hard time to find in the first place on the web.

May 2012
Sun Mon Tue Wed Thu Fri Sat
 << <   > >>
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31    

Search

Custom Search

XML Feeds

What is RSS?

Who's Online?

  • Guest Users: 1

powered by
b2evolution