473,407 Members | 2,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Static inline functions with static local variables


C89 doesn't have inline but I wanted to use inline in my C89-compliant
program. Someone suggested to me that I use macros to determine
whether inline was supported, and if necessary, simply define it like:

#define inline /* nothing */

, and then define the functions within header files with static
inline:

static inline void Func(void) {}

Something was nagging at me though, some reason why the static-ness of
the function would make it somehow different from plain old inline.
When total it finally occured to me:

static inline void Func(void)
{
static int count = 0;
}

*With* static, each translation unit has its own "count". WithOUT
static, there's only one "count" for the entire program.

Anyway just thought I'd share. . .

Martin

Oct 6 '07 #1
5 6492

Martin Wells wrote:
C89 doesn't have inline but I wanted to use inline in my C89-compliant
program. Someone suggested to me that I use macros to determine
whether inline was supported, and if necessary, simply define it like:

#define inline /* nothing */

, and then define the functions within header files with static
inline:

static inline void Func(void) {}

Something was nagging at me though, some reason why the static-ness of
the function would make it somehow different from plain old inline.
When total it finally occured to me:

static inline void Func(void)
{
static int count = 0;
}

*With* static, each translation unit has its own "count". WithOUT
static, there's only one "count" for the entire program.

Anyway just thought I'd share. . .
>From n1256.pdf:
6.7.4 (3):

An inline de nition of a function with external linkage shall not
contain a de nition of a
modi able object with static storage duration, and shall not contain a
reference to an
identi er with internal linkage.

Oct 6 '07 #2
On Sat, 06 Oct 2007 05:05:24 -0700, santosh wrote:

>>From n1256.pdf:

6.7.4 (3):

An inline de nition of a function with external linkage shall not
contain a de nition of a
modi able object with static storage duration, and shall not contain a
reference to an
identi er with internal linkage.
I see that they haven't stopped using the one-character fi
ligature, which what's more got lost in the way of your post from
you to me.
--
Army1987 (Replace "NOSPAM" with "email")
A hamburger is better than nothing.
Nothing is better than eternal happiness.
Therefore, a hamburger is better than eternal happiness.

Oct 6 '07 #3
santosh:
6.7.4 (3):

An inline definition of a function with external linkage shall not
contain a defnition of a
modifable object with static storage duration, and shall not contain a
reference to an
identifer with internal linkage

Ah I see. C++ differs from C in that you can have static-duration
objects within an extern inline function.

Martin

Oct 6 '07 #4
Army1987 wrote:
On Sat, 06 Oct 2007 05:05:24 -0700, santosh wrote:

>>>From n1256.pdf:

6.7.4 (3):

An inline de nition of a function with external linkage shall not
contain a de nition of a
modi able object with static storage duration, and shall not contain
a reference to an
identi er with internal linkage.

I see that they haven't stopped using the one-character fi
ligature, which what's more got lost in the way of your post from
you to me.
Yes. It was displayed fine in my newsreader's editor. Guess I'll have to
manually replace them. It would be nice if WG14 could provide a HTML
version of their drafts, if not all then at least important ones like
n1256.pdf.

Oct 6 '07 #5
Army1987 wrote:
santosh wrote:
>From n1256.pdf:

6.7.4 (3):

An inline de nition of a function with external linkage shall not
contain a de nition of a modi able object with static storage
duration, and shall not contain a reference to an identi er with
internal linkage.

I see that they haven't stopped using the one-character fi
ligature, which what's more got lost in the way of your post from
you to me.
That, to my knowledge, is peculiar to the Foxread PDF displayer.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Oct 6 '07 #6

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

Similar topics

9
by: Bryan Parkoff | last post by:
I have noticed that C programmers put static keyword beside global variable and global functions in C source codes. I believe that it is not necessary and it is not the practice in C++. Static...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
13
by: jamihuq | last post by:
Can someone please tell me what I am doing wrong. I wrote this static inline function to test another function and I am getting errors which make no sense to me. thanks Jami /* test2.h */ ...
4
by: Sean | last post by:
I am a little confused by the "extern inline and static inline" rules. I understand that "extern inline" guarantees no function storage is created (all are inlined). But the following test seems to...
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
3
by: Steve Folly | last post by:
Hi, I had a problem in my code recently which turned out to be the 'the "static initialization order fiasco"' problem (<http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.12>) The FAQ...
14
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared...
2
by: Nagrik | last post by:
Dear Group, The book of Bjarne Stroustrup in chapter 5.4.4 says the following "The word static is one of the most overused words in C and C++. For static data members it has both of the...
17
by: Juha Nieminen | last post by:
As we know, the keyword "inline" is a bit misleading because its meaning has changed in practice. In most modern compilers it has completely lost its meaning of "a hint for the compiler to inline...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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...
0
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.