delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/1999/03/15/09:20:07

Mailing-List: contact cygwin-help AT sourceware DOT cygnus DOT com; run by ezmlm
Sender: cygwin-owner AT sourceware DOT cygnus DOT com
Delivered-To: mailing list cygwin AT sourceware DOT cygnus DOT com
Message-ID: <11ad01be6eee$c7a47bf0$e63d2509@jonpryor.raleigh.ibm.com>
From: "Jonathan Pryor" <jonpryor AT vt DOT edu>
To: "Cygwin Mailing List" <cygwin AT sourceware DOT cygnus DOT com>
Subject: egcs 1.1.1: odd warning message with template friend functions
Date: Mon, 15 Mar 1999 09:18:33 -0500
MIME-Version: 1.0
X-Priority: 3
X-MSMail-Priority: Normal
X-Mailer: Microsoft Outlook Express 4.72.3110.5
X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3110.3

------=_NextPart_000_11AA_01BE6EC4.CC9E62D0
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

Note: I'm not complaining about the resulting executable; I'm
complaining about the warning message itself.

When I compile the attached source code, I get the 
following warning:

    mi.cpp:7: warning: friend declaration `bool operator ==(
        const class meta_info<T> &, const class meta_info<T> &)'
    mi.cpp:7: warning:   declares a non-template function
    mi.cpp:7: warning:   (if this is not what you intended, make sure
    mi.cpp:7: warning:   the function template has already been declared,
    mi.cpp:7: warning:   and add <> after the function name here)
    mi.cpp:8: warning: friend declaration `class meta_info<T> metaid(
        const T &)'
    mi.cpp:8: warning:   declares a non-template function

The final executable runs fine; it's just that this message doesn't 
make sense to me.  The line that causes the warning is the following:

    template <typename T>
    class meta_info {
        friend bool operator== ( const meta_info<T>&, 
            const meta_info<T>& );
        /* ... */
    };

Since the friend declaration is in a template class, I fail
to see how `operator==' could *not* be a template function,
which the warning message suggests.

Furthermore, in Stroustrup, 3rd Edition (pages 854-855), he
has a similar example (a template class with a friend declaration),
and states that because the friend declaration was in a template
class, the friend must also be a template.  In this example,
the friend declaration did *not* have `<>' appended to the function
name.

If I follow the compilers suggestion and modify the class
declaration to:

    template <typename T>
    class meta_info {
        // note `<>' after `operator=='
        friend bool operator==<> ( const meta_info<T>&, 
            const meta_info<T>& );
        /* ... */
    };

egcs will compile this without any error messages, but now
MSVC6 has caniptions...  In addition, it seems to go against
what was stated in Stroustrup, so I'm finding this...bizarre.

(Yes, we could argue that it's a bug in MSVC -- except that
Stroutstrup doesn't mention needing to add a `<>' to make
the friend a template.  In addition, MSVC compiles the original
version without any errors/warnings -- so (to me) it doesn't 
make sense to call it an MSVC bug.)

Again, this doesn't seem to cause any problems in the executable.
I just don't like warning messages that seem to be incorrect, or,
at the very least, don't make any sense for this circumstance.

In a related question: Should I to send future compiler bug 
reports to the cygwin mailing list, to the egcs-bugs mailing
list, or to both?  I didn't see any reference to this on the
web site, so I'm just "playing this by ear"...

Thanks,
 - Jon

------=_NextPart_000_11AA_01BE6EC4.CC9E62D0
Content-Type: application/octet-stream;
	name="mi.cpp"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
	filename="mi.cpp"

// Odd egcs behavior with template-friend functions.

#include <stdio.h>

template <typename T>
class meta_info {
  friend bool operator=3D=3D<> ( const meta_info<T>&, const =
meta_info<T>& );
  friend meta_info<T> metaid<> ( const T& );

  meta_info<T>& operator=3D(const meta_info<T>& );  // no assignment.
  meta_info ( const meta_info<T>& );              // no cpy ctor
  meta_info() {}                                  // only friends can =
create=20
                                                  //  the class.
public:

};

template <typename T>
bool operator=3D=3D ( const meta_info<T>&, const meta_info<T>& ) {
  return false;
}

template <typename T>
inline meta_info<T> metaid ( const T& ) {
  meta_info<T> mi;
  return mi;
}

class meta_info<int> {
public:
  static long max () throw() {
    return 42;
    // should be: return std::numeric_limits<int>::max();
  }
};


int main (int, char **) {
  int n =3D 5;
  printf ("meta_info<int>::max() is: %i\n", metaid(n).max() );
  return 0;
}



------=_NextPart_000_11AA_01BE6EC4.CC9E62D0
Content-Type: text/plain; charset=us-ascii

--
Want to unsubscribe from this list?
Send a message to cygwin-unsubscribe AT sourceware DOT cygnus DOT com
------=_NextPart_000_11AA_01BE6EC4.CC9E62D0--

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019