由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - inline到底能省多少时间?
相关主题
c++中的inline 函数是做什么的?c++ define 一问
c++ inline问题 (转载)C++小插曲
A C++ compiler related interview question码工试题 (转载)
c++ class definitionc++ does not check const for extern variable?
关于inline function里的static variableinline functions in C++
A tech question (转载)inline C function in VC++ 2005 express
C++方法全都内联有什么坏处?请教C++里面这个表达
static variable in template headerC语言的变量都一定要放在stack上吗?
相关话题的讨论汇总
话题: inline话题: compiler话题: linkage话题: function话题: odr
进入Programming版参与讨论
1 (共1页)
g*********s
发帖数: 1782
1
多少行的函数就没必要inline了?
在改一个程序。.h里的inline函数太多,稍微改动一下就要重新编译半天,很烦。想直
接全挪到.cpp
里去。
P********e
发帖数: 2610
2
要看使用频率吧.
也没省多少.exe还变大.

【在 g*********s 的大作中提到】
: 多少行的函数就没必要inline了?
: 在改一个程序。.h里的inline函数太多,稍微改动一下就要重新编译半天,很烦。想直
: 接全挪到.cpp
: 里去。

M**u
发帖数: 10158
3
不一定省时间

【在 g*********s 的大作中提到】
: 多少行的函数就没必要inline了?
: 在改一个程序。.h里的inline函数太多,稍微改动一下就要重新编译半天,很烦。想直
: 接全挪到.cpp
: 里去。

S**I
发帖数: 15689
4
这个问题compiler说了算。

【在 g*********s 的大作中提到】
: 多少行的函数就没必要inline了?
: 在改一个程序。.h里的inline函数太多,稍微改动一下就要重新编译半天,很烦。想直
: 接全挪到.cpp
: 里去。

g*********s
发帖数: 1782
5
compiler的准则是啥?

【在 S**I 的大作中提到】
: 这个问题compiler说了算。
S**I
发帖数: 15689
6
这你得去看compiler的文档。

【在 g*********s 的大作中提到】
: compiler的准则是啥?
g*********s
发帖数: 1782
7
i don't care about the implementation details.
there should just be some principles, i guess. do you know?
in addition, i don't think compiler can change an in-class defined method
to be non-inline.

【在 S**I 的大作中提到】
: 这你得去看compiler的文档。
M**u
发帖数: 10158
8
read C++ faq

【在 g*********s 的大作中提到】
: i don't care about the implementation details.
: there should just be some principles, i guess. do you know?
: in addition, i don't think compiler can change an in-class defined method
: to be non-inline.

t****t
发帖数: 6806
9
keyword "inline" is like keyword "register". they are HINT, not restriction.

【在 g*********s 的大作中提到】
: i don't care about the implementation details.
: there should just be some principles, i guess. do you know?
: in addition, i don't think compiler can change an in-class defined method
: to be non-inline.

p*p
发帖数: 75
10
现在64位的编译器非常aggressive,不管你要不要,所以不用多想了。
相关主题
A tech question (转载)c++ define 一问
C++方法全都内联有什么坏处?C++小插曲
static variable in template header码工试题 (转载)
进入Programming版参与讨论
g*********s
发帖数: 1782
11
but if you put the method in .h with inline, it is always honored, isn't
it?

restriction.

【在 t****t 的大作中提到】
: keyword "inline" is like keyword "register". they are HINT, not restriction.
S**I
发帖数: 15689
12
of course not

【在 g*********s 的大作中提到】
: but if you put the method in .h with inline, it is always honored, isn't
: it?
:
: restriction.

t****t
发帖数: 6806
13
i said it's a hint.

【在 g*********s 的大作中提到】
: but if you put the method in .h with inline, it is always honored, isn't
: it?
:
: restriction.

g*********s
发帖数: 1782
14
so if you put the following in class_x.h that is included by a couple of
.cpp, the compiler possibly treats X::f() as non-inline? Isn't that an ODR
violation?
// class_x.h
class X {
public:
void f();
}
inline
void X::f(){}

【在 t****t 的大作中提到】
: i said it's a hint.
t****t
发帖数: 6806
15
that's not your concern.

【在 g*********s 的大作中提到】
: so if you put the following in class_x.h that is included by a couple of
: .cpp, the compiler possibly treats X::f() as non-inline? Isn't that an ODR
: violation?
: // class_x.h
: class X {
: public:
: void f();
: }
: inline
: void X::f(){}

g*********s
发帖数: 1782
16
more confused now.
so inline keyword is a message from the coder to the compiler about two
things: 1) code expansion, which is a suggestion. the compiler can
either honor it or not; 2) external linkage, which is a requirement.
otherwise odr is violated.
but if the compiler doesn't honor 1), how could it meet 2)?
the following article says inline functions by default have external
linkage. is it possible to use another type of linkage? how?
http://stackoverflow.com/questions/4193639/inline-function-link
When the function in the header is not inline, then multiple definitions
of this function (e.g. in multiple translation units) is a violation of
ODR rules.
Inline functions by default have external linkage. Hence, as a
consequence of ODR rules (given below), such multiple definitions (e.g.
in multiple translation units) are Okay:
$3.2/5- "There can be more than one definition of a class type (Clause
9), enumeration type (7.2), inline function with external linkage
(7.1.2), class template (Clause 14), non-static function template
(14.5.6), static data member of a class template (14.5.1.3), member
function of a class template (14.5.1.1), or template specialization for
which some template parameters are not specified (14.7, 14.5.5) in a
program provided that each definition appears in a different translation
unit, and provided the definitions satisfy the following requirements.
Given such an entity named D defined in more than one translation unit,
then each definition of D shall consist of the same sequence of tokens;
and [...]
How the linker treats inline functions is a pretty much implementation
level detail. Suffice it to know that the implementation accepts such
mulitple defintions within the limitations of ODR rules
Note that if the function declaration in header is changed to 'static
inline....', then the inline function explicitly has internal linkage
and each translation unit has it's own copy of the static inline
function.

of
ODR

【在 g*********s 的大作中提到】
: so if you put the following in class_x.h that is included by a couple of
: .cpp, the compiler possibly treats X::f() as non-inline? Isn't that an ODR
: violation?
: // class_x.h
: class X {
: public:
: void f();
: }
: inline
: void X::f(){}

g*********s
发帖数: 1782
17
my concerns are:
1) is inline always a hint, or in some situation a requirement.
2) how much time could it save if the hint is honored.

【在 t****t 的大作中提到】
: that's not your concern.
t****t
发帖数: 6806
18
inline is a property of function. that is fixed.
whether the compiler actually do expand the function body or not is solely
the decision of compiler.
as for "external" requirement, it doesn't mean the compiler has to export a
function. external solely means the name can be referred within other
translation units. any implementation satisfying that is ok.

【在 g*********s 的大作中提到】
: my concerns are:
: 1) is inline always a hint, or in some situation a requirement.
: 2) how much time could it save if the hint is honored.

y*d
发帖数: 2226
19

C++的compiler比你想象的神奇得多
in class defined照样可以给你搞成不inline
所有90年代以后的C++ compiler (准确的说是linker)都能处理一个函数在多个
compiling unit里出现多个版本的问题(为了支持template)。in class的function搞成非
inline,一点难度都没有

【在 g*********s 的大作中提到】
: i don't care about the implementation details.
: there should just be some principles, i guess. do you know?
: in addition, i don't think compiler can change an in-class defined method
: to be non-inline.

y*d
发帖数: 2226
20

a
前半段勉强说得通
后半段不对
extern和export是两码事
extern是说declare不define/implement一个variable
whether a symbol can be referred from another compiling unit在C语言里是由
static决
定的,by default所有东西都是全局可见,加了static只在当前unit可见。
你的"external"我没听说过,VC里有个叫export的东西,跟你说的有点像,是控制dll
的符号在外面能
不能看见

【在 t****t 的大作中提到】
: inline is a property of function. that is fixed.
: whether the compiler actually do expand the function body or not is solely
: the decision of compiler.
: as for "external" requirement, it doesn't mean the compiler has to export a
: function. external solely means the name can be referred within other
: translation units. any implementation satisfying that is ok.

相关主题
c++ does not check const for extern variable?请教C++里面这个表达
inline functions in C++C语言的变量都一定要放在stack上吗?
inline C function in VC++ 2005 expressstrcat()
进入Programming版参与讨论
y*d
发帖数: 2226
21

ODR是说你不能乱来,不是说compiler不能
举个例子
template
T add(const T& a, const T& b)
{
return a+b;
}
这个函数你放在.h里,有十个cpp里用过add()
那就有十个.o或者.obj都含有add
但是link的时候,会把多余的干掉
80年代的template实现要把所有的function都inline,还不吃static variable
但是90年代以后编译器就已经灰墙强悍了
ODR是为了帮你检查错误,怕你写了两个不同的define
对编译器自己,没这么多规矩
再举个例子,有虚函数的class都有个vtable
override和rtti都靠这个
请问这个vtable是在哪个.o里?
答案是每个.o里都有一个。因为编译器编译这个文件的时候,不知道别的.o里是否已经
有了,所以只能
是先放进去一个再说
然后呢?
link的时候,编译器会删掉多余的,只留一个

【在 g*********s 的大作中提到】
: so if you put the following in class_x.h that is included by a couple of
: .cpp, the compiler possibly treats X::f() as non-inline? Isn't that an ODR
: violation?
: // class_x.h
: class X {
: public:
: void f();
: }
: inline
: void X::f(){}

y*d
发帖数: 2226
22

省多少时间,取决于你的编译器和你具体的程序,难说得很
有时候是dramatic的,多数情况下意思不大

【在 g*********s 的大作中提到】
: 多少行的函数就没必要inline了?
: 在改一个程序。.h里的inline函数太多,稍微改动一下就要重新编译半天,很烦。想直
: 接全挪到.cpp
: 里去。

t****t
发帖数: 6806
23
i didn't mean extern-> export, OP implied that. by "external" i (and OP)
meant external linkage. by contrast, function/object with static keyword is
internal linkage, auto objects has no linkage.
for C, mostly that's the case. but for c++, linkage rules are more
complicated, since you have const, inline, anonymous namespace, etc.
the "export" my previous post is not VC export. it's merely some
implementation term. as for you said "extern是说declare不define/implement一
个variable", that's of course not correct. you may say "extern int i=1;",
which is a declaration AND a definition.

dll

【在 y*d 的大作中提到】
:
: 省多少时间,取决于你的编译器和你具体的程序,难说得很
: 有时候是dramatic的,多数情况下意思不大

y*d
发帖数: 2226
24

g++ says:
warning: 'i' initialized and declared 'extern'
你用的啥诡异compiler

【在 t****t 的大作中提到】
: i didn't mean extern-> export, OP implied that. by "external" i (and OP)
: meant external linkage. by contrast, function/object with static keyword is
: internal linkage, auto objects has no linkage.
: for C, mostly that's the case. but for c++, linkage rules are more
: complicated, since you have const, inline, anonymous namespace, etc.
: the "export" my previous post is not VC export. it's merely some
: implementation term. as for you said "extern是说declare不define/implement一
: 个variable", that's of course not correct. you may say "extern int i=1;",
: which is a declaration AND a definition.
:

t****t
发帖数: 6806
25
that's not a good practice, i am just showing an example. you may try "
extern const int = 1; ".

【在 y*d 的大作中提到】
:
: g++ says:
: warning: 'i' initialized and declared 'extern'
: 你用的啥诡异compiler

g*******e
发帖数: 3013
26
只有测试才可以告诉你。

【在 g*********s 的大作中提到】
: 多少行的函数就没必要inline了?
: 在改一个程序。.h里的inline函数太多,稍微改动一下就要重新编译半天,很烦。想直
: 接全挪到.cpp
: 里去。

1 (共1页)
进入Programming版参与讨论
相关主题
C语言的变量都一定要放在stack上吗?关于inline function里的static variable
strcat()A tech question (转载)
C语言重复定义问题C++方法全都内联有什么坏处?
求GCC高手static variable in template header
c++中的inline 函数是做什么的?c++ define 一问
c++ inline问题 (转载)C++小插曲
A C++ compiler related interview question码工试题 (转载)
c++ class definitionc++ does not check const for extern variable?
相关话题的讨论汇总
话题: inline话题: compiler话题: linkage话题: function话题: odr