473,490 Members | 2,695 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Any preprocessor definition to tell if inline is supported?



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

Sep 24 '07 #1
9 2107
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;
}
Sep 24 '07 #2
Martin Wells <wa****@eircom.netwrites:
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--
Sep 24 '07 #3
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

Sep 24 '07 #4
On Mon, 24 Sep 2007 22:30:25 +0200, Michal Nazarewicz wrote:
Martin Wells <wa****@eircom.netwrites:
>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.
Sep 24 '07 #5
On Sep 24, 12:18 pm, Martin Wells <war...@eircom.netwrote:
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.

Sep 24 '07 #6
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.)
Sep 24 '07 #7
In article <11**********************@k79g2000hse.googlegroups .com>,
user923005 <dc*****@connx.comwrote:
>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.
Sep 24 '07 #8
user923005 wrote On 09/24/07 17:27,:
On Sep 24, 12:18 pm, Martin Wells <war...@eircom.netwrote:
>>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.

--
Er*********@sun.com

Sep 24 '07 #9
Martin Wells <wa****@eircom.netwrote:
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
Sep 25 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
4254
by: qazmlp | last post by:
// test.h class test { public : inline void check() ; } ; // test.C inline void test::check() // Is 'inline' optional here?
9
3732
by: Dave H | last post by:
Hello, I have a query regarding definition lists. Is it good practice semantically to use the dt and dd elements to mark up questions and answers in a frequently asked questions list, or FAQ? ...
4
1932
by: Jim Ford | last post by:
I have a single C file with the following code: int f2() { /* Blah-blah */ } int f1() { /* Blah-blah */
9
3637
by: Walter Roberson | last post by:
I have run into a peculiarity with SGI's C compiler (7.3.1.2m). I have been reading carefully over the ANSI X3.159-1989 specification, but I cannot seem to find a justification for the behaviour....
7
44271
by: Erik Leunissen | last post by:
L.S. How can I make certain code parts be compiled conditionally, depending on the definition of a macro such as: #define VERSION "2.3" Is it all right to do things like:
2
6616
by: Paolo | last post by:
I imported a VC++6.0 project into VC++7.1. The conversion operation makes a mess with Preprocessor Definitions, adding a "$(NoInherit)" for each file. For example: I had a DLL project in VC++6.0...
13
464
by: josh | last post by:
Hi, I've read that in the future the C++ will not more support the preprocessor and now some preprocessor functionality have already been implemented in the language itself. What are that? ...
36
2079
by: anon.asdf | last post by:
Hello! Can the proprocessor make conditional decisions. Here's an example of the functionality (not standard C!) #define PUT_BYTE(const_index, val) \ #preprocessor_if (const_index ==...
31
426
by: Madhur | last post by:
I have problems faced in adding definitions in the command header file. I have defined a header file which includes huge set of global constants and I am using them in all the C files. For...
0
6974
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7146
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7183
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6852
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7356
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4573
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3084
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
628
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
277
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.