Linking problems with templates.

Mumit Khan khan@nanotech.wisc.edu
Tue Nov 30 23:39:00 GMT 1999


"Chalapathi R. Emani" <cemani@qualcomm.com> writes:
> 
> Hi,
> 
> I am trying to use the latest version from  cygwin and 
> I am having linking problems with a file that uses templates:
> 
> When I compiled one of the source  file I got the following warnings:
> 
> Config.h:178: warning: friend declaration `class istream & operator >>(class
> ream &, class OrderedTriple<T> &)'
> Config.h:178: warning:   declares a non-template function
> Config.h:178: warning:   (if this is not what you intended, make sure
> Config.h:178: warning:   the function template has already been declared,
> Config.h:178: warning:   and add <> after the function name here)
> Config.h:179: warning: friend declaration `class ostream & operator
> <<(class ost
> ream &, class OrderedTriple<T> &)'
[ ... ]

This is a change in the standard that had caught most (all?) of us
who've had C++ code dating back years. You need to fix the code, and
there's really no easy way out. 

How to keep compatibility with other compilers? Well, I usually use
configure-driven systems, and I wrote a simple autoconf macro (now
also used by other packages such as GNU Octave) that checks for if
your C++ compiler supports friend template declaration or not, and
if not, defines a pre-processor macro to support older compilers
(such as GCC-2.8.1 or MSVC 6.x).

I believe this was added in egcs-1.1 (egcs-2.91.57), but I may be off 
by a release.

Here's an example:

  # ifdef CXX_NO_FRIEND_TMPL_DECL
  #   define LTGT
  # else
  #   define LTGT <>
  # endif

Then in the code:

  // must forward class and friend functions 
  template <typename T>
  class Foo;

  template <typename T> Foo<T>&
  operator += (Foo<T>&, const T&);

  template <class T>
  class Foo 
  {
  public:

    // ...
    friend Foo<T>& operator += LTGT (Foo<T>&, const T&);
    // ...
  };

For new compilers, nothing needs to be done. For old ones, define the
macro CXX_NO_FRIEND_TMP_DECL when compiling.

> 
> I went ahead and compiled this with the flag "-fguiding-decls"   and these
> warnings have gone away.
> Then I tried linking this with the rest of the objects and libstdc++.a and
> I am getting the following 
> errors:

Well, unfortunately -fguiding-decls really doesn't work when there are
libraries involved. I suppose with lots of extra work, it could be made
to work always, but I doubt it'll be done unless someone contributes
the changes.

> 
> On Solaris when I use the "-fguiding-decls"  option  and compile and link
> it, it builds just fine.
> May be because I use 2.8.1 on Solaris.

2.8.1 didn't really support many of the new C++ standard features, so it's
not surprising.

> 
> Do you know if the above option has been deprecated in   Cygwin 2.95.2  ?
> 

Not deprecated, just full of pitfalls.

For more details of template friends, see Section 14.5.3 [temp.friend]/1
in the C++ ISO standard document.

Regards,
Mumit


--
Want to unsubscribe from this list?
Send a message to cygwin-unsubscribe@sourceware.cygnus.com



More information about the Cygwin mailing list