static inline functions

J

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)
 
T

Tomás

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
 
A

Alf P. Steinbach

* 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).
 
J

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. 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
 
A

Alf P. Steinbach

* 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.
 
J

JE

jamihuq said:
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
 
I

Ian Collins

jamihuq said:
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?
 
F

Fei Liu

--------------------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)
 
F

Frank Schmidt

jamihuq said:
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
 
A

Alf P. Steinbach

* David Lindauer:
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).
 
F

Fei Liu

David said:
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.
 
J

Jay_Nabonne

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
 
J

jamihuq

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top