Topic: non-member friend functions which are inlined
Author: rfg@netcom.com (Ronald F. Guilmette)
Date: Wed, 29 Sep 1993 09:03:35 GMT Raw View
In article <1993Sep21.215043.8601@vitsemi.COM> tsai@vitsemi.COM (Jodi Tsai) writes:
>
>I'm trying to compile the following code using DEC C++.
>
>class String
>{
> ... /* irrelevant stuff deleted */
> friend void cat(const String&, const String&, String&);
>};
>
>inline void cat(const String& x, const String& y, String& r)
>{
> r.rep = Scat(r.rep, x.chars(), x.length(), y.chars(), y.length());
>}
>
>and I'm getting the error...
>
>
>../g++-include/String.h:524: error: In this declaration, "cat" is declared with\
> both internal and external linkage.
>
>The same code compiles successfully using g++ and Sun C++ 2.1.
>
>1. Is this error message inconsistent with the C++ standard?
There ain't no such thing (yet).
Bute regardless of that, your question is interesting, and may bear on
the *future* C++ standard.
The first declaration of your `cat' function (in the friend declaration)
causes that function to have "external linkage" (I think).
The second declaration however is inline, which (I believe) then tries to
give the same function "static linkage".
It is fairly clear that the function must have only one of these two kinds
of linkage, but I'm not sure which one it should have in this case, nor am
I at all sure whether or not the compiler may (or must) issue a diagnostic
for such cases.
If anyone else knows the answers, please speak up.
(Follow-ups to comp.std.c++ please.)
--
-- Ronald F. Guilmette ------------------------------------------------------
------ domain address: rfg@netcom.com ---------------------------------------
------ uucp address: ...!uunet!netcom.com!rfg -------------------------------
Author: daniels@biles.com (Brad Daniels)
Date: Wed, 29 Sep 1993 19:38:44 GMT Raw View
In article <rfgCE3yHz.14t@netcom.com>,
Ronald F. Guilmette <rfg@netcom.com> wrote:
>In article <1993Sep21.215043.8601@vitsemi.COM> tsai@vitsemi.COM (Jodi Tsai) writes:
>>
>>I'm trying to compile the following code using DEC C++.
>>
>>class String
>>{
>> ... /* irrelevant stuff deleted */
>> friend void cat(const String&, const String&, String&);
>>};
>>
>>inline void cat(const String& x, const String& y, String& r)
>>{
>> r.rep = Scat(r.rep, x.chars(), x.length(), y.chars(), y.length());
>>}
>>
>>and I'm getting the error...
>>
>>
>>../g++-include/String.h:524: error: In this declaration, "cat" is declared with\
>> both internal and external linkage.
...
>The first declaration of your `cat' function (in the friend declaration)
>causes that function to have "external linkage" (I think).
>
>The second declaration however is inline, which (I believe) then tries to
>give the same function "static linkage".
I believe the declaration is in error. In any case, you can change the
friend declaration to be:
friend inline void cat(const String&, const String&, String&);
to fix the error. The modified code will work (at least) with DEC C++ and
HP C++. Requiring the inline keyword in this context certainly seems
reasonable, though I can't find any text in the ARM to support either side
of the question.
- Brad
--
----------------------------------------------------------------------
+ Brad Daniels | "Let others praise ancient times; +
+ Biles and Associates | I am glad I was born in these." +
+ These are my views, not B&A's | - Ovid(43 B.C - 17 A.D) +
Author: simonh@swidev.demon.co.uk (Simon Huntington)
Date: Fri, 1 Oct 1993 17:14:21 +0000 Raw View
In article <rfgCE3yHz.14t@netcom.com> rfg@netcom.com writes:
>In article <1993Sep21.215043.8601@vitsemi.COM> tsai@vitsemi.COM (Jodi Tsai)
> writes:
>>
>>I'm trying to compile the following code using DEC C++.
>>
>>class String
>>{
>> ... /* irrelevant stuff deleted */
>> friend void cat(const String&, const String&, String&);
>>};
>>
>>inline void cat(const String& x, const String& y, String& r)
>>{
>> r.rep = Scat(r.rep, x.chars(), x.length(), y.chars(), y.length());
>>}
>>
>>and I'm getting the error...
>>
>>
>>../g++-include/String.h:524: error: In this declaration, "cat" is declared
> with\
>> both internal and external linkage.
The ARM says an inline function 'has default internal linkage'. I take this
to mean the same as it does for const objects: the symbol has internal
linkage unless it was previously given external linkage.
The ARM contradicts this though on pg.98.
For a non-member function an inline specified is equivalent to a static
specifier for linkage purposes.
Which would imply the code is incorrect. If this is the case, its another
part of the standard that is geared towards the C separate compilation model
that is totally useless for C++. An inline function should be available to
other modules if it is declared as extern. inline is a decision in the
implementation of the function, nothing to do with linkage.
So, I don't care what the standard says. I say it should be legal and the
compiler should be able to retrieve the inline-function without the user
messing about with header files so its available in every module.
--
Simon Huntington
Software Interrupt Developments. Leeds, UK.