473,395 Members | 1,656 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,395 software developers and data experts.

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 31011
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
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
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
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
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
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
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
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
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
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.