Topic: Can standard functions be inline?


Author: "Pavel Kuznetsov" <su41vo7la001@sneakemail.com>
Date: Fri, 26 Oct 2001 06:14:36 GMT
Raw View
Terence Kelling wrote:
> Would it be standard conformant to have these
> functions [like isalpha] defined as inline if compiled under C++?

Barry Margolin wrote:
> If they were, how would you tell the difference?  And if you can't
> tell the difference, how could it be prohibited?

Well, I suppose you can't take the address of a macro.
So yes, one could determine whether isalpha is defined as inline
function or as a macro at compile time.

--
Pavel Kuznetsov

[ THE REPLY ADDRESS IS NOT GUARANTEED TO LIVE LONGER THAN FEW WEEKS! ]
[ The permanent address is: pavel <dot> kuznetsov <at> mail <dot> ru ]


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: James Dennett <jdennett@acm.org>
Date: Fri, 26 Oct 2001 17:13:16 GMT
Raw View
Pavel Kuznetsov wrote:

> Terence Kelling wrote:
>
>>Would it be standard conformant to have these
>>functions [like isalpha] defined as inline if compiled under C++?
>>
>
> Barry Margolin wrote:
>
>>If they were, how would you tell the difference?  And if you can't
>>tell the difference, how could it be prohibited?
>>
>
> Well, I suppose you can't take the address of a macro.
> So yes, one could determine whether isalpha is defined as inline
> function or as a macro at compile time.

The question was not whether you can tell the difference
between a macro and a function -- trivially you can do
that with #ifdef.  The question is whether you can detect
in portable code whether a function has been declared as
inline, and you cannot; the only effect of inline apart
from being an hint to the optimizer is to suppress what
would otherwise be ODR violations.

So, to answer the original question: yes, functions such
as isalpha can be defined as inline under C++.

-- James Dennett

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: Barry Margolin <barmar@genuity.net>
Date: Fri, 26 Oct 2001 17:14:25 GMT
Raw View
In article <9ratjr$se0sg$1@ID-97366.news.dfncis.de>,
Pavel Kuznetsov <su41vo7la001@sneakemail.com> wrote:
>Terence Kelling wrote:
>> Would it be standard conformant to have these
>> functions [like isalpha] defined as inline if compiled under C++?
>
>Barry Margolin wrote:
>> If they were, how would you tell the difference?  And if you can't
>> tell the difference, how could it be prohibited?
>
>Well, I suppose you can't take the address of a macro.
>So yes, one could determine whether isalpha is defined as inline
>function or as a macro at compile time.

The standard specifies that if it's defined as a macro it must *also* be a
function.  When you take the address, you get the address of the function
(function-like macros are only expanded when followed by (<args>), and
&isalpha wouldn't have any parameters, so the macro would be ignored).

--
Barry Margolin, barmar@genuity.net
Genuity, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: terencekelling@cs.com (Terence Kelling)
Date: Fri, 26 Oct 2001 17:14:41 GMT
Raw View
"Pavel Kuznetsov" <su41vo7la001@sneakemail.com> wrote in message news:<9ratjr$se0sg$1@ID-97366.news.dfncis.de>...
>
> Well, I suppose you can't take the address of a macro.
> So yes, one could determine whether isalpha is defined as inline
> function or as a macro at compile time.
>

Actually, this is handled in Standard C90 in section 7.1.7 "Any
function declared in a header may be additionally implemented as a
macro defined in the header, so a library function should not be
declared explicitly if its header is included.  Any macro definition
of a function can be suppressed locally by enclosing the name of the
function in parentheses, because the name is then not followed by the
left parenthesis that indicates expansion of a macro function name.
For the same syntactic reason, it is permitted to take the address of
a library function even if it is also defined as a macro." Footnote 95
then goes on to explicitly state "This means that an implementation
must provide an actual function for each library function, even if it
also provides a macro for that function."

How this issue in standard C++ was addressed was the major point in my
initial posting.  As long as it is required that taking the address of
an inline function always yields the same result even in seperate
translation units of a complete program, there would be no problem.

Terence Kelling

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: comeau@panix.com (Greg Comeau)
Date: Sat, 27 Oct 2001 02:11:30 GMT
Raw View
In article <c0696667.0110251850.f509aac@posting.google.com>,
Terence Kelling <terencekelling@cs.com> wrote:
>I suppose my major concern about this was if someone tried to take the
>address of a standard function that was declared inline.  I wasn't
>sure what the ramifications of that would be.  However, I read in a
>recent posting that the standard requires that there be a unique
>address for inline functions and static variables declared within
>them.  While this might be difficult to implement, it would certainly
>address this issue.

Well, yeah, assuming we're talking about extern inlines and such.
--
Greg Comeau         export ETA: December     See our Oct 31st special
Comeau C/C++ ONLINE ==>     http://www.comeaucomputing.com/tryitout
World Class Compilers:  Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: comeau@panix.com (Greg Comeau)
Date: Sat, 27 Oct 2001 02:11:34 GMT
Raw View
In article <9ratjr$se0sg$1@ID-97366.news.dfncis.de>,
Pavel Kuznetsov <su41vo7la001@sneakemail.com> wrote:
>Well, I suppose you can't take the address of a macro.
>So yes, one could determine whether isalpha is defined as inline
>function or as a macro at compile time.

You could determine if it were a macro.  But you can't determine
if it's (perhaps also) a function that's inline.
--
Greg Comeau         export ETA: December     See our Oct 31st special
Comeau C/C++ ONLINE ==>     http://www.comeaucomputing.com/tryitout
World Class Compilers:  Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: Martin von Loewis <loewis@informatik.hu-berlin.de>
Date: Mon, 29 Oct 2001 16:13:19 GMT
Raw View
terencekelling@cs.com (Terence Kelling) writes:

> How this issue in standard C++ was addressed was the major point in my
> initial posting.

I'm still not certain whether you were asking a question, and if so,
what the question was, or whether you were pointing out a problem or a
feature of C++.

If the question was "Can standard functions be inline?", you've
already seen the answer by now, which is "yes".

If you were asking "Why does C++ disallows to declare the standard
functions as macros, when taking their address isn't a problem in C?",
then the answer is "Because macros wouldn't be in namespace std."

If you were asking "Can you take the address of an inline function in
C++", the answer is also "yes".

If you were asking something else, please ask again.

Regards,
Martin

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: Steve Clamage <clamage@eng.sun.com>
Date: Wed, 31 Oct 2001 20:21:49 GMT
Raw View
On Fri, 26 Oct 2001, Terence Kelling wrote:
>
> How this issue in standard C++ was addressed was the major point in my
> initial posting.  As long as it is required that taking the address of
> an inline function always yields the same result even in seperate
> translation units of a complete program, there would be no problem.

The standard has that requirement, but not all current compilers
get it right.

--
Steve Clamage, stephen.clamage@sun.com

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: terencekelling@cs.com (Terence Kelling)
Date: Wed, 24 Oct 2001 23:06:04 GMT
Raw View
I am specifically refering to those functions that are included from
the <cname> or <name.h> headers.  For example, in <cctype> or
<ctype.h> there are several functions like isalpha which are
implemented as both functions and macros.  But I believe that the C++
standard requires that these be functions even if the C standard says
they can be macros.  Would it be standard conformant to have these
functions defined as inline if compiled under C++?

Thanks,
Terence Kelling

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: comeau@panix.com (Greg Comeau)
Date: Thu, 25 Oct 2001 16:38:19 GMT
Raw View
In article <c0696667.0110240933.2f1a354b@posting.google.com>,
Terence Kelling <terencekelling@cs.com> wrote:
>I am specifically refering to those functions that are included from
>the <cname> or <name.h> headers.  For example, in <cctype> or
><ctype.h> there are several functions like isalpha which are
>implemented as both functions and macros.  But I believe that the C++
>standard requires that these be functions even if the C standard says
>they can be macros.

That sounds right.

>Would it be standard conformant to have these
>functions defined as inline if compiled under C++?

Standard C++ takes great pains to lay out its requirements,
as well as to document the so-called abstract C++ machine,
however, it also allows for observable behavior, meaning that
for the most part, so long as the program gets correct results
and correct output, then it can do what it wants.  So yes,
it can inline those functions, or not even call them at all
if it figures something else out.
--
Greg Comeau         export ETA: December     See our Oct 31st special
Comeau C/C++ ONLINE ==>     http://www.comeaucomputing.com/tryitout
World Class Compilers:  Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: Martin von Loewis <loewis@informatik.hu-berlin.de>
Date: Thu, 25 Oct 2001 16:38:34 GMT
Raw View
terencekelling@cs.com (Terence Kelling) writes:

> I am specifically refering to those functions that are included from
> the <cname> or <name.h> headers.  For example, in <cctype> or
> <ctype.h> there are several functions like isalpha which are
> implemented as both functions and macros.  But I believe that the C++
> standard requires that these be functions even if the C standard says
> they can be macros.

Correct.

> Would it be standard conformant to have these functions defined as
> inline if compiled under C++?

Certainly. I cannot see anything in the standard prohibiting such an
implementation, and I cannot see any problems for application coming
from such an implementation.

inline was introduced into the language primarily to allow changing
all those macros into functions without loss of efficiency.

Regards,
Martin

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: Barry Margolin <barmar@genuity.net>
Date: Thu, 25 Oct 2001 18:14:45 GMT
Raw View
In article <c0696667.0110240933.2f1a354b@posting.google.com>,
Terence Kelling <terencekelling@cs.com> wrote:
>I am specifically refering to those functions that are included from
>the <cname> or <name.h> headers.  For example, in <cctype> or
><ctype.h> there are several functions like isalpha which are
>implemented as both functions and macros.  But I believe that the C++
>standard requires that these be functions even if the C standard says
>they can be macros.  Would it be standard conformant to have these
>functions defined as inline if compiled under C++?

If they were, how would you tell the difference?  And if you can't tell the
difference, how could it be prohibited?

--
Barry Margolin, barmar@genuity.net
Genuity, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: terencekelling@cs.com (Terence Kelling)
Date: Fri, 26 Oct 2001 06:14:15 GMT
Raw View
I suppose my major concern about this was if someone tried to take the
address of a standard function that was declared inline.  I wasn't
sure what the ramifications of that would be.  However, I read in a
recent posting that the standard requires that there be a unique
address for inline functions and static variables declared within
them.  While this might be difficult to implement, it would certainly
address this issue.

Terence Kelling

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]