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.
Related
This question already has answers here:
c++ inline function?
(6 answers)
Why are C++ inline functions in the header?
(8 answers)
Inline qualifier stems from prototype or definition?
(4 answers)
Why must an inline function be "defined" in a header if the compiler can just inline functions on its own accord
(4 answers)
Closed 4 months ago.
I'm a little confused about the use of header-only files in cpp .
From my understanding, I need to declare all functions and variables with "inline". For classes/structs, I don't need to apply inline because it's already declared implicit.
Example:
In app.hpp file
namespace app{
inline void func1(){#do sth};
inline int num;
class application{
void func2();//this should be fine;
void int num2;
}
}
Why it's necessary to declare all variables and functions with inline? It works properly if I declare function in .hpp and define functions in .cpp files without inline, but I have to do header-only.If not using inline, it will shows multiple-definition error.(If I declare with static instead of inline, it shows not error at compile time but shows wrong outputs when I run it)
Thanks for anybody's help!
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:
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.
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.
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.