This question already has answers here:
When should I write the keyword 'inline' for a function/method?
(16 answers)
Closed 9 years ago.
I am trying to completely understand what inline does but I get confused when it comes to constructors. So far I understood that inlining a function will put the function in the place it is called:
class X {
public:
X() {}
inline void Y() { std::cout << "hello" << std::endl; }
};
int main() {
X x;
x.Y; //so instead of this it will be: "std::cout << "hello" << std::endl;"
}
But what will this do (how does this get replaced?):
class X {
public:
//or inline if the compilers feeling friendly for this example
__forceinline X() {}
}
The meaning of inline is just to inform the compiler that the finction in question will be defined in this translation as well as possibly other translation units. The compiler uses this information for two purposes:
It won't define the function in the translation unit as an externally visible, unique symbol, i.e., you won't get an error about the symbol being defined multiple times.
It may change its view on whether it wants to call a function to access the finctionality or just expand the code where it is used. Whether it actually will inline the code will depend on what the compiler can inline, what it thinks may be reasonable to inline, and possibly on the phase of the moon.
There is no difference with respect to what sort of function is being inlined. That is, whether it is a constructor, a member function, a normal function, etc.
BTW, member functions defined inside the class definition are implicitly declared inline. There is no need to mention it explicitly. __forecedinline seems to be a compiler extension and assuming it does what it says is probably a bad idea as compilers are a lot better at deciding on what should be inlined than humans.
Related
This question already has answers here:
When should I write the keyword 'inline' for a function/method?
(16 answers)
Closed 1 year ago.
I've been reading a c++ book, C++ Primer, and i was going through the class features and everything, and i encountered that, in a class most functions ( or every) are inline automatically.
What difference does it really make? explicitly defining an inline function vs implicitly defining an inline function, we could have already overloaded them inside the class scope anyways, i am finding it very difficult to understand this part. Is there any kind of performance gain by doing explicitly ?. in the photo we can see get() function using both methods explicit & implicit, can someone clarify me here.
I have to edit the question, because I've been told so many times that class members functions are not automatically inline. But the book c++ primer and other internet sources say that they are automatically inline.
Here is a piece of text from the book..
If a definition of a member function is within the class body it is implicitly inline. If you only declare it in the class body and you place the definition outside of it you need to make it explicitly inline.
This can be done in two ways:
struct test {
inline void foo();
};
void test::foo() {
}
Or
struct test {
void foo();
};
inline void test::foo() {
}
While both work, the second option is generally recommended.
This question already has answers here:
When should I write the keyword 'inline' for a function/method?
(16 answers)
Closed 7 years ago.
I am refactoring a big legacy source file "big.cpp", which contains several class definitions, used solely in this file. e.g., in big.cpp
class A {
inline void func1() {
// bla bla ...
}
void func2() {
// bla bla ...
}
}
some functions are explicitly with in-line keyword, some are not.
As these classes are only in cpp file, not even in header file, it is quite a mess and not possible to unit test, etc. so I am trying to split it into smaller files, as "a.h", "a.cpp"; Then I have a concern. after refactoring, shall these functions be treated as inline functions or not? e.g., I guess func1() shall be inlined, but what about func2()?
I am afraid, if some former inline functions are changed to non-inline, their performance will be slower, so I have to be careful.
If you define a member function inside a class like func2 in your example, the inline is implied.
This question already has answers here:
Defining a member function inside the class instead of outside in C++?
(4 answers)
Closed 7 years ago.
Is it true that if a function body is defined inside a class, the compiler will mark it inline? (even if not marked by the writer)
example:
class F {
public:
void func() {
std::cout << "is this inline?\n";
}
};
Yes.
[C++14: 9.3/2]: A member function may be defined (8.4) in its class definition, in which case it is an inline member function (7.1.2), or it may be defined outside of its class definition if it has already been declared but not defined in its class definition. [..]
However, whether this has any observable effects beyond the associated linkage requirements is only as predictable as the inline keyword ever is.
The reason for this rule is so that it is legal to include the class definition — member functions and all — via a header into multiple translation units. You would have multiple reference linker errors otherwise.
If you state the body of a function inside a class, it is equivalent to defining that function outside the class and prefixing it with the inline keyword, but whether or not it will be inlined is ultimately up to the compiler.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Is it true that redefinition mean that we're trying to define an entity which is already defined. This question appear from the following code example:
int a=5;
int main()
{
int a=3;//redefinition? I think no, because `int a` denote an entity different from the global "a"
}
and one more example:
int foo(){ return 1; }
int main()
{
int foo();
int a=foo();//Now a is 1
}
We can't define just declared foo() function inside the main() function body, but if we can will it be a redefinition?
Local variables might shadow global ones, that's what the :: scope resolution operator is for
#include <iostream>
using namespace std;
int a=5;
int main()
{
int a=3;
cout << a; // 3
cout << ::a; // 5
}
so no ODR problems here.
As for the second example the function declaration inside another function (when it doesn't get confused by the most-vexing-parse), I recommend this question: Is there a use for function declarations inside functions?
And: no, you can't redefine your function inside main(). You can redeclare it (even with different parameters, thus declaring a new function) but that doesn't mean you can define it so.
There's an excellent excerpt from the wiki page which I recommend to read:
In short, the ODR states that:
In any translation unit, a template, type, function, or object can
have no more than one definition. Some of these can have any number of
declarations. A definition provides an instance.
In the entire
program, an object or non-inline function cannot have more than one
definition; if an object or function is used, it must have exactly one
definition. You can declare an object or function that is never used,
in which case you don't have to provide a definition. In no event can
there be more than one definition.
Some things, like types, templates,
and extern inline functions, can be defined in more than one
translation unit. For a given entity, each definition must be the
same. Non-extern objects and functions in different translation units
are different entities, even if their names and types are the same.
Some violations of the ODR must be diagnosed by the compiler. Other
violations, particularly those that span translation units, are not
required to be diagnosed.1
No, when dealing with redefinition, it is important to remember SCOPE. It only applies for two variables with the same name that are defined in the SAME SCOPE
In example 1, the second a is LOCAL SCOPE and is local to the function. Therefore, that is the a that is viewed and referred to until you exit the function body
No. int a = foo(); or int a = 3; , inside main(), is a new variable that is also called a.
A redefinition is an attempt to redefine the same variable, e.g.:
int a = 5;
int a = 6;
Also
int foo();
is not a definition. It's a declaration. A function definition includes { }.
Redefinition is somewhat what leads to compiler-time error.
For example:
int a;
bool a;
or
void f();
int f;
In your case there wasn't compiler-time error. It was about name hiding, scope and resolving rules.
int a = 5;
{
int a = 6; //because of { } internal a is in other scope and can be defined without error
int b = a; //b == 6
}
int b = a; //b == 5
In the last case you have two diffrent "a", each in own scope of program.
In one point of program if you use a name such "a", there is only one entity that is behind this name. If compiler can't find the best match for "a" between diffrent variants you get redifinition and error.
The first one is not a redefinition due to different scopes, just like you thought.
The second one is a redeclaration, but it's ok to redeclare something any number of times you want, though the joke gets stale with repetition.
If you allow definition of functions inside functions, you get to write all the semantics, because there ain't such yet (aside from lambdas).
For someone who did so, look at the GCC C compiler, "nested functions" and "statement expressions".
Anyway, redefintion would be an error due to the One Definition Rule.
This question already has answers here:
What exactly is the "as-if" rule?
(3 answers)
Closed 8 years ago.
If i have a simple class like this
class A {
void private_function();
public:
void public_function() { /* calls the private function in here */ }
};
Is the compiler required to emit object code for private_function(), or is it allowed to inline all calls to private_function() and to omit private_function from the generated executable?
Is the compiler required to emit object code for private_function()
It will have to if anything uses its address.
or is it allowed to inline all calls to private_function() and to omit private_function from the generated executable?
If nothing uses its address, yes. The program's behaviour would be identical whether or not it generated an unused non-inline version; so by the "as if" rule, it's free not to generate it.