473,498 Members | 2,021 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

static inline functions

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 */

static inline unsigned int test3(unsigned int aValue)
{
aValue++;
return aValue;
}

--------------------Configuration: test2 - Win32
Debug--------------------
Compiling...
Text2.c
error C2054: expected '(' to follow 'inline'
error C2085: 'test3' : not in formal parameter list
error C2143: syntax error : missing ';' before '{'

error C2054: expected '(' to follow 'inline'
error C2085: 'test3' : not in formal parameter list
error C2143: syntax error : missing ';' before '{'
Error executing cl.exe.

test2.exe - 6 error(s), 0 warning(s)

Feb 28 '06 #1
13 31026
jamihuq posted:
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 */

static inline unsigned int test3(unsigned int aValue)
{
aValue++;
return aValue;
}

I am WAY open to correction here, but defining an inline function as
"static" would only have a noticible effect if you use a static variable
inside it. You don't have static variable/object, so the "static" keyword is
redundant, and can only serve to annoy the compiler and reduce the
likelihood of optimization.

One of the magic rules of inline functions is that, even though you can have
multiple definitions of them across source files, there's only ever one
instance of their static variables. For instance:

inline unsigned GetNumber()
{
static unsigned only_one_of_me_in_the_universe = 0;

return ++i;
}
If you stick the above function in a header file, and include it in a
hundred source files, there'll still only ever be one static variable.

However... I wonder what happens when you stick "static" in front of
"inline".

(Time for a hypothesis:)

It would suggest to me that it makes the function "unique" among translation
units... and therefore I would place a modest bet that our magic rule of
static variables no longer applies. Just a hypothesis though! I might test
it tomorrow -- it's 12:21am here in Ireland and I should be in bed...
-Tomás

--------------------Configuration: test2 - Win32
Debug--------------------
Compiling...
Text2.c
error C2054: expected '(' to follow 'inline'
error C2085: 'test3' : not in formal parameter list
error C2143: syntax error : missing ';' before '{'

error C2054: expected '(' to follow 'inline'
error C2085: 'test3' : not in formal parameter list
error C2143: syntax error : missing ';' before '{'
Error executing cl.exe.

test2.exe - 6 error(s), 0 warning(s)


Feb 28 '06 #2
* jamihuq:
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 */

static inline unsigned int test3(unsigned int aValue)
{
aValue++;
return aValue;
}

--------------------Configuration: test2 - Win32
Debug--------------------
Compiling...
Text2.c
error C2054: expected '(' to follow 'inline'
error C2085: 'test3' : not in formal parameter list
error C2143: syntax error : missing ';' before '{'

error C2054: expected '(' to follow 'inline'
error C2085: 'test3' : not in formal parameter list
error C2143: syntax error : missing ';' before '{'
Error executing cl.exe.

test2.exe - 6 error(s), 0 warning(s)


Most probably you have some earlier error, e.g. a class definition
lacking a terminating semicolon.

Otherwise you're using an UnGood compiler.

Btw., style: write '++aValue', which better reflects the intent, and
'inline static' (ditto), and just 'unsigned' ('int' is superflous).

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Feb 28 '06 #3
Actually, defining any function as static tells the compiler that the
function can only be used in the file the function was defined in. So,
I defined the function inline in test2.h and used it in test2.c. So,
you can't use the function in main.c directly.

Now , I did a simple test. I placed the inline function in test2.c
only. I get the following errors:
Compiling...
test2main.c
Test2.c
rror C2054: expected '(' to follow 'inline'
rror C2085: 'test3' : not in formal parameter list
rror C2143: syntax error : missing ';' before '{'
warning C4013: 'test3' undefined; assuming extern returning int
Error executing cl.exe.

Which is 3 less errors than placing the inline function in test2.h.

Very strange.

But that's not the issue. The issue is why are there errors in a simple
inline function like this. I am compiling in MSVC++ 6.0.

Thanks
Jami

Feb 28 '06 #4
* jamihuq:
Actually, defining any function as static tells the compiler that the
function can only be used in the file the function was defined in.
Sorry, that's incorrect.
[snip] I am compiling in MSVC++ 6.0.


For other reasons that your function, you should upgrade; MSVC 6.0 is
not a very standard-conforming compiler, especially wrt. templates.

Regarding your function, try it in a _minimal_ program.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Feb 28 '06 #5
JE

jamihuq wrote:
Actually, defining any function as static tells the compiler that the
function can only be used in the file the function was defined in. So,
I defined the function inline in test2.h and used it in test2.c. So,
you can't use the function in main.c directly.

Now , I did a simple test. I placed the inline function in test2.c
only. I get the following errors:
Compiling...
test2main.c
Test2.c
rror C2054: expected '(' to follow 'inline'
rror C2085: 'test3' : not in formal parameter list
rror C2143: syntax error : missing ';' before '{'
warning C4013: 'test3' undefined; assuming extern returning int
Error executing cl.exe.

Which is 3 less errors than placing the inline function in test2.h.

Very strange.

But that's not the issue. The issue is why are there errors in a simple
inline function like this. I am compiling in MSVC++ 6.0.

Thanks
Jami


What happens if you put a semi-colon immediately above your function?

And please don't use _static_ except inside functions and
classes...especially not in a header.

JE

Feb 28 '06 #6
jamihuq wrote:
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 */

static inline unsigned int test3(unsigned int aValue)
{
aValue++;
return aValue;
}

Is this a class member or a stand alone function? If the later, why
static in a header?

Either way, it should compile, which compiler are you using?

--
Ian Collins.
Feb 28 '06 #7
--------------------Configuration: test2 - Win32
Debug--------------------
Compiling...
Text2.c
error C2054: expected '(' to follow 'inline'
error C2085: 'test3' : not in formal parameter list
error C2143: syntax error : missing ';' before '{'

error C2054: expected '(' to follow 'inline'
error C2085: 'test3' : not in formal parameter list
error C2143: syntax error : missing ';' before '{'
Error executing cl.exe.

test2.exe - 6 error(s), 0 warning(s)
test2.h
static inline unsigned test3(unsigned aValue);
static inline unsigned test3(unsigned aValue){
return ++aValue;
}
test2.cpp
#include "test2.h"
#include <iostream>

int main(void){
unsigned x = 3;
cout << test3(x) << endl;
}

I used GNU g++ 2.96 and things worked out fine here. So I suggest you
look carefully at your code and also try your code with another
compiler.
g++ -v
Reading specs from /usr/lib/gcc-lib/i586-mandrake-linux/2.96/specs
gcc version 2.96 20000731 (Linux-Mandrake 8.0 2.96-0.48mdk)

Feb 28 '06 #8

"jamihuq" <ja*********@yahoo.com> schrieb im Newsbeitrag
news:11*********************@t39g2000cwt.googlegro ups.com...
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 */

static inline unsigned int test3(unsigned int aValue)
{
aValue++;
return aValue;
}

--------------------Configuration: test2 - Win32
Debug--------------------
Compiling...
Text2.c
error C2054: expected '(' to follow 'inline'
error C2085: 'test3' : not in formal parameter list
error C2143: syntax error : missing ';' before '{'

error C2054: expected '(' to follow 'inline'
error C2085: 'test3' : not in formal parameter list
error C2143: syntax error : missing ';' before '{'
Error executing cl.exe.

test2.exe - 6 error(s), 0 warning(s)


you are in the wrong group... and inline is imho not supported in c, at
least not when msvc6 was created

Feb 28 '06 #9


"Alf P. Steinbach" wrote:
* jamihuq:
Actually, defining any function as static tells the compiler that the
function can only be used in the file the function was defined in.


Sorry, that's incorrect.


what does it mean if you declare an inline function static?

Thanks,

David
Feb 28 '06 #10
* David Lindauer:

"Alf P. Steinbach" wrote:
* jamihuq:
Actually, defining any function as static tells the compiler that the
function can only be used in the file the function was defined in.

Sorry, that's incorrect.


what does it mean if you declare an inline function static?


Rather, what does it mean if you declare a static function inline.

In that case 'inline' serves no other purpose than as a very vague
optimization hint, which a modern compiler is likely to ignore.

However, the reasons I wrote 'incorrect' above were (1) that the
standard has no notion of file whatsoever, and (2) that an inline static
function can be used in every compilation unit in which it is defined
(e.g. by having some common definition of it #include'd).

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Feb 28 '06 #11
David Lindauer wrote:

what does it mean if you declare an inline function static?

Thanks,

David


Declare a function 'static inline' means each definition of the
function is unique and multiple translation units can each have their
own definition of the function and compilation will still work. In the
final executable file, every required copy of the function object code
is included but will be assigned and loaded at different virtual
address.

Feb 28 '06 #12
On Mon, 27 Feb 2006 16:05:01 -0800, jamihuq wrote:
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.

--------------------Configuration: test2 - Win32
Debug--------------------
Compiling...
Text2.c


Try calling it "Text2.cpp".

- Jay

Feb 28 '06 #13
Thanks to all posters, but I found why I am getting errors. Actually,
Jay, Fei and some others made me look past the code itself. It seems
that MSVC 6.0 isn't update with the latest C standards. GNU C compiler
is updated to understand inline functions.

After I named the files as .cpp I didn't get any errors or warnings.
Thanks to all and I am now ending this topic.

Jami

Feb 28 '06 #14

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

Similar topics

9
6342
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...
4
7220
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...
32
3001
by: lcdgoncalves | last post by:
Hi everyone Is there a real need to use keyword static with functions, if we simply don't declare their prototypes in .h file? Many textbooks avoid to discuss this matter and/or discuss only...
3
5827
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...
2
3136
by: DaTurk | last post by:
Hi, I have an interesting issue, well, it's not really an issue, but I'd like to understand the mechanics of what's going on. I have a file, in CLI, which has a class declared, and a static...
5
6498
by: Martin Wells | last post by:
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...
2
3761
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...
5
5026
by: ciccio | last post by:
Hi, I have a problem with my code that the compiler does not find any inline functions which are static. The simple code example is written below, this is what the compiler throws at me. ]...
17
8349
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
7121
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7162
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
7197
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
6881
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
7375
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
5456
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,...
1
4899
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3088
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...
0
287
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.