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
19 楼
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
20 楼
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
21 楼
现在64位的编译器非常aggressive,不管你要不要,所以不用多想了。
g*s
22 楼
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
23 楼
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
24 楼
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
25 楼
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
26 楼
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
27 楼
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
28 楼
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
29 楼
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
30 楼
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
31 楼
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.
y*d
32 楼
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(){}
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.
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
36 楼
that's not a good practice, i am just showing an example. you may try " extern const int = 1; ".