静态内联函数 [英] static inline functions

查看:163
本文介绍了静态内联函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以告诉我我做错了什么。我写了这个静态

内联函数来测试另一个函数,我收到的错误

对我没用。


谢谢

Jami


/ * test2.h * /


静态内联unsigned int test3(unsigned int aValue )

{

aValue ++;

返回aValue;

}


--------------------配置:test2 - Win32

调试-------------- ------

编译......

Text2.c

错误C2054:预期''(''跟随''内联''

错误C2085:''test3'':不在正式参数列表中

错误C2143:语法错误:在''{'之前缺少'';'' '


错误C2054:预期''(''跟随''内联''

错误C2085:''test3'':不在形式参数中list

错误C2143:语法错误:在''{''之前缺少'';'''
执行cl.ex时出错e。

test2.exe - 6个错误,0个警告

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)

推荐答案

jamihuq发布:
jamihuq posted:
有人可以告诉我我做错了什么。我写了这个静态内联函数来测试另一个函数,我收到的错误对我来说毫无意义。

感谢
Jami

/ * test2.h * /

静态内联unsigned int test3(unsigned int aValue)
{
aValue ++;
返回aValue;
}



我现在可以修改,但是将内联函数定义为

" static"如果在其中使用静态变量

,则只会产生明显的影响。你没有静态变量/对象,所以静态对象。关键字是

冗余,只能用来惹恼编译器并降低优化的可能性。


其中一个内联函数的神奇规则是,尽管你可以在源文件中对它们进行多次定义,但它们的静态变量只有一个

实例。例如:


inline unsigned GetNumber()

{

static unsigned only_one_of_me_in_the_universe = 0;

返回++ i;

}

如果您将上述函数粘贴在头文件中,并将其包含在
$ b $中b百源文件,仍然只有一个静态变量。


但是......我想知道当你坚持使用静态时会发生什么。在

内联前面。


(假设的时间:)


它会建议对我来说它使功能独特在翻译中

单位...因此我会谨慎地下注我们的魔法规则

静态变量不再适用。只是一个假设!我明天可能会测试一下它的价格 - 这是12点21分在爱尔兰,我应该在床上......

-Tomás


--------------------配置:test2 - Win32
调试-------------- ------
编译......
Text2.c
错误C2054:预期''(''跟随''内联''
错误C2085:'' test3'':不在正式参数列表中
错误C2143:语法错误:缺少'';''之前''{''

错误C2054:expected''(''要关注''inline''
错误C2085:''test3'':不在正式参数列表中
错误C2143:语法错误:缺少'';''之前''{''
错误执行cl.exe。

test2.exe - 6个错误,0个警告
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)






* jamihuq:
* jamihuq:
有人可以告诉我我做错了什么。我写了这个静态
内联函数来测试另一个函数我是ge对我来说没有任何意义的错误。

感谢
Jami

/ * test2.h * /

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

--------- -----------配置:test2 - Win32
调试--------------------
编译...... < br> Text2.c
错误C2054:预期''(''跟随''内联''
错误C2085:''test3'':不在正式参数列表中
错误C2143 :语法错误:错过'';''在''{''
错误之前C2054:预期''(''跟随''内联''
错误C2085:''test3 '':不在正式参数列表中
错误C2143:语法错误:在''{'''错误执行cl.exe之前缺少'';''。

test2。 exe - 6个错误,0个警告
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)




很可能你有一些早期错误,例如一个类定义

缺少一个终止分号。


否则你使用的是UnGood编译器。


顺便说一下,样式:写''++ aValue'',它更好地反映了意图,并且

''内联静态''(同上),只是''unsigned''(''int ''是超级的。)


-

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么这么糟糕?

A:热门发布。

问:usenet和电子邮件中最烦人的是什么?



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?


实际上,将任何函数定义为static告诉编译器

函数只能在函数定义的文件中使用。所以,

我在test2.h中定义了内联函数,并在test2.c中使用它。所以,

你不能直接在main.c中使用这个函数。


现在,我做了一个简单的测试。我将内联函数放在test2.c

中。我收到以下错误:

编译...

test2main.c

Test2.c

rror C2054 :期望''(''跟随''内联''

rror C2085:''test3'':不在正式参数列表中

rror C2143:语法错误:缺少'';''之前''{''

警告C4013:''test3''未定义;假设extern返回int

执行cl.exe时出错。 />

比在test2.h中放置内联函数少3个错误。


非常奇怪。


但这不是问题。问题是为什么在这样的简单的

内联函数中会出现错误。我正在编译MSVC ++ 6.0。


谢谢

Jami

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


这篇关于静态内联函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆