Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Any preprocessor definition to tell if inline is supported?

Reply
Thread Tools

Any preprocessor definition to tell if inline is supported?

 
 
Martin Wells
Guest
Posts: n/a
 
      09-24-2007


Is there anything like __inline_is_supported to indicate whether a
certain C compiler supports inline?

I'm writing my code as fully portable C89, but I want to use inline.

Martin

 
Reply With Quote
 
 
 
 
=?iso-2022-kr?q?Harald_van_D=0E=29=26=0Fk?=
Guest
Posts: n/a
 
      09-24-2007
On Mon, 24 Sep 2007 12:18:10 -0700, Martin Wells wrote:
> Is there anything like __inline_is_supported to indicate whether a
> certain C compiler supports inline?
>
> I'm writing my code as fully portable C89, but I want to use inline.


No C89 compiler supports inline. They're not allowed to, since C89 allows
programmers to name their own objects and functions "inline". In other
words, this program is valid C89:

#include <stdio.h>
int main(int inline, char *argv[]) {
if (inline >= 2)
puts(argv[1]);
return 0;
}

Some compilers support something that looks like C89, but adds inline.
These are not C89 compilers. They refuse to compile valid programs such
as the above.

You can, however, remain portable by writing in the common subset of C89
and C99, and then you can use

#if __STDC_VERSION__ < 199901L
#define inline /* nothing */
#endif

static inline int zero(void) {
return 0;
}
 
Reply With Quote
 
 
 
 
Michal Nazarewicz
Guest
Posts: n/a
 
      09-24-2007
Martin Wells <(E-Mail Removed)> writes:

> Is there anything like __inline_is_supported to indicate whether a
> certain C compiler supports inline?
>
> I'm writing my code as fully portable C89, but I want to use inline.


I use the following myself:

#ifndef __GNUC__
# if __STDC_VERSION__ + 0 >= 199901L
# define __inline__ inline
# define __restrict__ restrict
# else
# define __inline__
# define __restrict__
# endif
#endif

__inline__ and __restrict__ identifiers are reserved though so it may
have some portability issues. __inline__ and __restrict__ keywords come
from GCC. It allows using them even if you are compiling your program
in C89 mode where there is no inline keyword.

--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl>--<jid:mina86*jabber.org>--ooO--(_)--Ooo--
 
Reply With Quote
 
Martin Wells
Guest
Posts: n/a
 
      09-24-2007
Harald:

> No C89 compiler supports inline. They're not allowed to, since C89 allows
> programmers to name their own objects and functions "inline". In other
> words, this program is valid C89:
>
> #include <stdio.h>
> int main(int inline, char *argv[]) {
> if (inline >= 2)
> puts(argv[1]);
> return 0;
>
> }
>
> Some compilers support something that looks like C89, but adds inline.
> These are not C89 compilers. They refuse to compile valid programs such
> as the above.



While these "C89 + extras compilers" may fail to compile the code you
show above, it's not inconceivable that a compliant C compiler could
work with the following:

inline int Func(void) { return 7; }

int main(void)
{
int inline = 6;

return 0;
}

There's already plenty of different C keywords that have different
meanings in different contexts. (Granted none of them can be used as
identifiers, but you get the picture).


> You can, however, remain portable by writing in the common subset of C89
> and C99, and then you can use
>
> #if __STDC_VERSION__ < 199901L
> #define inline /* nothing */
> #endif
>
> static inline int zero(void) {
> return 0;



I'd be hesitant to use static, given that inline functions are, by
default, extern. Off the top of my head I can't think of any drawbacks
of making them static, but I know from past experience not to jump
into something just because I can't see the drawbacks off the top of
my head.

Can anyone see any drawbacks of making an inline function static?
(Except for being able to declare but not define it in another
translation unit).

And by the way, I do realise I could use further macro trickery to get
rid of the static.

Martin

 
Reply With Quote
 
=?iso-2022-kr?q?=1B=24=29CHarald_van_D=0E=29=26=0Fk?=
Guest
Posts: n/a
 
      09-24-2007
On Mon, 24 Sep 2007 22:30:25 +0200, Michal Nazarewicz wrote:
> Martin Wells <(E-Mail Removed)> writes:
>
>> Is there anything like __inline_is_supported to indicate whether a
>> certain C compiler supports inline?
>>
>> I'm writing my code as fully portable C89, but I want to use inline.

>
> I use the following myself:
>
> #ifndef __GNUC__
> # if __STDC_VERSION__ + 0 >= 199901L
> # define __inline__ inline
> # define __restrict__ restrict
> # else
> # define __inline__
> # define __restrict__
> # endif
> #endif
>
> __inline__ and __restrict__ identifiers are reserved though so it may
> have some portability issues. __inline__ and __restrict__ keywords come
> from GCC. It allows using them even if you are compiling your program
> in C89 mode where there is no inline keyword.


You can go the other way around:

#if __STDC_VERSION__ >= 199901L
/*
* Uncomment this if you like:
#define inline inline
#define restrict restrict
*/
#elif defined(__GNUC__)
#define inline __inline__
#define restrict __restrict__
#elif defined(__Special_Other_Compiler__)
#define inline __inline
#define restrict /* nothing */
#else
#define inline /* nothing */
#define restrict /* nothing */
#endif

This, strictly speaking, still isn't valid C89, but it will work using
implementation extensions on GCC and SOC, and other C89 implementations
that don't define __GNUC__ or __Special_Other_Compiler__ must accept it
since you don't define any reserved identifiers this way. Additionally,
it is valid C99.

The reason it still isn't valid C89, is because an implementation is
allowed to define __GNUC__ without supporting any GNU extensions, and
similarly for SOC. Judging from what you're currently using, you don't
care about supporting such an implementation. I wouldn't either.

By the way, you don't need __STDC_VERSION__ + 0. Macros that are not
defined expand to 0 in #if expressions.
 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      09-24-2007
In article <(E-Mail Removed) .com>,
user923005 <(E-Mail Removed)> wrote:

>For compilers that offer inline, many allow you to set a compiler
>option to "inline any suitable function" which will not require any
>decoration of the function name and will also probably guess better
>than you will what to inline.


On the other hand, declaring your functions inline may cause them to
be inlined on compilers that other people use to compile your program,
and whose options you don't know.

-- Richard



--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
 
Reply With Quote
 
user923005
Guest
Posts: n/a
 
      09-24-2007
On Sep 24, 12:18 pm, Martin Wells <(E-Mail Removed)> wrote:
> Is there anything like __inline_is_supported to indicate whether a
> certain C compiler supports inline?
>
> I'm writing my code as fully portable C89, but I want to use inline.


Inline is just a compiler hint. The compiler is free to ignore you.

For compilers that offer inline, many allow you to set a compiler
option to "inline any suitable function" which will not require any
decoration of the function name and will also probably guess better
than you will what to inline.

 
Reply With Quote
 
=?iso-2022-kr?q?Harald_van_D=0E=29=26=0Fk?=
Guest
Posts: n/a
 
      09-24-2007
On Mon, 24 Sep 2007 13:43:43 -0700, Martin Wells wrote:
> Harald:
>
>> No C89 compiler supports inline. They're not allowed to, since C89
>> allows programmers to name their own objects and functions "inline". In
>> other words, this program is valid C89:
>>
>> #include <stdio.h>
>> int main(int inline, char *argv[]) {
>> if (inline >= 2)
>> puts(argv[1]);
>> return 0;
>>
>> }
>>
>> Some compilers support something that looks like C89, but adds inline.
>> These are not C89 compilers. They refuse to compile valid programs such
>> as the above.

>
>
> While these "C89 + extras compilers" may fail to compile the code you
> show above, it's not inconceivable that a compliant C compiler could
> work with the following:
>
> inline int Func(void) { return 7; }
>
> int main(void)
> {
> int inline = 6;
>
> return 0;
> }


I'm not aware of any implementations that do this, but it's a fair point.
I don't believe there's any code that would break this way (although for
the other new keyword, restrict, there is).

> There's already plenty of different C keywords that have different
> meanings in different contexts. (Granted none of them can be used as
> identifiers, but you get the picture).
>
>> You can, however, remain portable by writing in the common subset of
>> C89 and C99, and then you can use
>>
>> #if __STDC_VERSION__ < 199901L
>> #define inline /* nothing */
>> #endif
>>
>> static inline int zero(void) {
>> return 0;

>
>
> I'd be hesitant to use static, given that inline functions are, by
> default, extern.


Non-inline functions are always extern or static, but inline functions
are by default neither.

> Off the top of my head I can't think of any drawbacks
> of making them static, but I know from past experience not to jump into
> something just because I can't see the drawbacks off the top of my head.


static inline int zero(void) { return 0; }
int foo(void) { return zero(); }

This calls the local zero function. It can be defined in as many units as
you like; since it's static, they won't conflict.

extern inline int zero(void) { return 0; }
int foo(void) { return zero(); }

This calls the local zero function. It can only be defined in this one
unit. If any other unit defines zero this way as well, it is an error.

inline int zero(void) { return 0; }
int foo(void) { return zero(); }

This calls either the local zero function, or some zero function defined
in another unit, depending on whether the compiler feels like inlining.
If no zero function is defined in another unit, it is an error, which may
go undetected.

(Note that this is what C99 gives you; it differs from certain C89
extensions to specify inlining.)
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      09-24-2007
user923005 wrote On 09/24/07 17:27,:
> On Sep 24, 12:18 pm, Martin Wells <(E-Mail Removed)> wrote:
>
>>Is there anything like __inline_is_supported to indicate whether a
>>certain C compiler supports inline?
>>
>>I'm writing my code as fully portable C89, but I want to use inline.

>
>
> Inline is just a compiler hint. The compiler is free to ignore you.
>
> For compilers that offer inline, many allow you to set a compiler
> option to "inline any suitable function" which will not require any
> decoration of the function name and will also probably guess better
> than you will what to inline.


"What he said." Also, be aware that the semantics
of `inline' in a C89-with-extras compiler may not be
exactly those used by a C99 compiler. Just knowing that
Frobozz Magic C can be made to recognize `inline' as a
keyword doesn't mean you can be sure what it means.

--
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      09-25-2007
Martin Wells <(E-Mail Removed)> wrote:

> Harald:
>
> > No C89 compiler supports inline. They're not allowed to, since C89 allows
> > programmers to name their own objects and functions "inline". In other
> > words, this program is valid C89:
> >
> > #include <stdio.h>
> > int main(int inline, char *argv[]) {
> > if (inline >= 2)
> > puts(argv[1]);
> > return 0;
> >
> > }


> While these "C89 + extras compilers" may fail to compile the code you
> show above, it's not inconceivable that a compliant C compiler could
> work with the following:
>
> inline int Func(void) { return 7; }
>
> int main(void)
> {
> int inline = 6;
>
> return 0;
> }


Possibly, but that would still leave cases like

/* Input is on the 4th line of the card */
#define inline 4

inline int func(void); /* Oops... */

Richard
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
CAN any one tell it whats the code tell it yogesh C++ 1 03-14-2007 01:12 PM
501 PIX "deny any any" "allow any any" Any Anybody? Networking Student Cisco 4 11-16-2006 10:40 PM
Compiler error occurred when try to use a flexible template expression in preprocessor definesCompiler error occurred when try to use a flexible template expression in preprocessor defines snnn C++ 6 03-14-2005 04:09 PM
preprocessor, token concatenation, no valid preprocessor token Cronus C++ 1 07-14-2004 11:10 PM
inline or not to inline in C++ Abhi C++ 2 07-03-2003 12:07 AM



Advertisments