friend templates and third party run-time debugger
Dimitri PAPADOPOULOS-ORFANOS
papadopo@shfj.cea.fr
Thu Jul 23 15:21:00 GMT 1998
Using egcs-1.0.3a, I tried to compile the following piece of code:
template <class T>
class Object
{
friend Object<T> foo(const Object<T>& x);
};
template <class T>
Object<T> foo(const Object<T>& x)
{
return x;
}
I got the following message:
$ g++ -c foo.cc
foo.cc:4: warning: friend declaration `class Object<T> foo(const class Object<T> &)'
foo.cc:4: warning: will not be treated as a template instantiation
foo.cc:4: warning: unless you compile with -fguiding-decls
foo.cc:4: warning: or add <> after the function name
$
I read the FAQ ( http://egcs.cygnus.com/faq.html#friend ) and found:
In order to make a specialization of a template function a friend
of a (possibly template) class, you must explicitly state that
the friend function is a template, by appending angle brackets to
its name, and this template function must have been declared
already. An error in the last public comment draft of the
ANSI/ISO C++ Standard has led people to believe that was not
necessary, but it is, and it was fixed in the final version of
the Standard.
So I rewrote the code:
template <class T> class Object;
template <class T> Object<T> foo(const Object<T>& x);
template <class T>
class Object
{
friend Object<T> foo<>(const Object<T>& x);
};
template <class T>
Object<T> foo(const Object<T>& x)
{
return x;
}
However, when I try to run a third party run-time debugger
(Parasoft's Insure++) on the code, but I either get a
"PARSER_ERROR" on larger pieces of code, or still the same
error as above once the above code is instrumented:
% insure -o foo foo.cc
foo.cc:7: warning: friend declaration `class Object<T> foo(const class Object<T> &)'
foo.cc:7: warning: will not be treated as a template instantiation
foo.cc:7: warning: unless you compile with -fguiding-decls
foo.cc:7: warning: or add <> after the function name
Now I try to use -fguiding-decls on the initial piece of code.
The GNU C++ manual says:
-fguiding-decls
Treat a function declaration with the same type as a potential
function template instantiation as though it declares that
instantiation, not a normal function. If a definition is given
for the function later in the translation unit (or another
translation unit if the target supports weak symbols), that
definition will be used; otherwise the template will be
instantiated. This behavior reflects the C++ language prior to
September 1996, when guiding declarations were removed. This
option implies `-fname-mangling-version-0', and will not work
with other name mangling versions.
In a previous posting, I exposed some problems I had with mangled
symbol names, which were too large for Sun's assembler. I was told
to use the -fsquangle option as soon as it is available - probably
release 1.1?
1) May I use -fguiding-decls and -fsquangle together?
2) Am I stuck until the bug in Insure++ is fixed?
Thanks,
--
Dimitri Papadopoulos
More information about the Gcc
mailing list