|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Hi Everyone,
I had this doubt for quite some while, does defining a member function within the class declaration makes it inline by default? Would the compiler attempt to inline all calls to such member functions? class sample { public: void print() { cout<<"sample::print function invoked"; } }; int main() { sample obj; obj.print(); return(0); } Thanks in advance!!! |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
Rahul wrote:
> Hi Everyone, > > I had this doubt for quite some while, does defining a member > function within the class declaration makes it inline by default? Yes, if you mean that it has the same efffect as using the inline keyword on the function. > Would the compiler attempt to inline all calls to such member > functions? It might do that, or not. The only guaranteed effect of the inline keyword in C++ is that there can be multiple definitions of the function in different translation units, even if that function has external linkage. Regarding optimization, the inline keyword is just a hint to the compilier that some compilers simply ignore, deciding on their own when to inline a function call and when not to. |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
>
> It might do that, or not. The only guaranteed effect of the inline keyword > in C++ is that there can be multiple definitions of the function in > different translation units, even if that function has external linkage. I didn't know that... do you have any links for further info on this one? |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
Rahul wrote:
>> It might do that, or not. The only guaranteed effect of the inline keyword >> in C++ is that there can be multiple definitions of the function in >> different translation units, even if that function has external linkage. > > I didn't know that... do you have any links for further info on this > one? The standard? -- Ian Collins. |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
Rahul wrote:
> I had this doubt for quite some while, does defining a member > function within the class declaration makes it inline by default? Yes, it does. > Would the compiler attempt to inline all calls to such member > functions? It depends on many factors and in reality virtually all of them are implementation-dependent. It might inline all calls, some of the calls, or none of the calls. The decision to inline can really be made on per-call basis. Moreover, the compiler might inline calls to the functions that are not explicitly declared 'inline' at its own discretion. For this reason, the relationship between the 'inline' specifier and the actual inlining of the calls might be (and is) significantly more loose then it is often assumed to be. As other already noted, the only thing the 'inline' specifier is guaranteed to do is to allow multiple definitions of the same function with external linkage in the program. -- Best regards, Andrey Tarasevich |
|
![]() |
| Outils de la discussion | |
|
|