Why inline is needed in Header only file in cpp [duplicate] - c++

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!

Related

In which object file will a function be defiined if it the function definition is within a class definition? [duplicate]

This question already has answers here:
Why does the same class being defined in multiple .cpp files not cause a linker multiple definition error?
(2 answers)
Closed 3 months ago.
I found out that if you put the function defintion outside of the class, but within the .h file, a Linker error will be thrown as the function is already defined in the main.obj. That makes sense. So, if I put the function defininition in the .cpp file of the class, the function will be deined within the class.obj. That makes sense too.
However, if I put the function definition WITHIN the class definition in the .h file, it works too. But in which .obj file is the function defined then?
Such as here:
#pragma once
class CTest
{
public:
int Subtract(int x, int y)
{
return x - y;
}
int Add(int x, int y)
{
return x + y;
}
};
So, is it going to be defined in the CTest.obj file or in the Main.obj file?
Thank you for your help.
In all of them. It's the linker's job to detect the multiple definitions and collapse them. A compiler might (and probably will) optimize this by only emitting the definition in TUs where the function is actually used.
Which is part of the reason why functions defined in headers are bad for compile time.

Why use explicit inline when every class member function is implicitly inlined [duplicate]

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.

Static variable in h-file C++ template library. Is it OK? [duplicate]

This question already has answers here:
How to have static data members in a header-only library?
(3 answers)
Closed 6 years ago.
I am developing lightweight parser as C++ h-file template library.
Gramma is described in specific BNF-like notation using overloaded operators on some classes which should be enumerated somehow. I need just one global variable as some counter performing it.
I do not want to use extern int var; in h-file and int var; in cpp-file because all my stuff in single header file and now the user just needs to include it.
I can declare static int var; in header file but copy of this variable appears in all object files where my header file is included.
Is it OK for template library?
Any suggestions?
As already mentioned you can use singleton pattern.
This version doesn't require definition of static member in template cpp file.
template <typename T> class Tmpl
{
public:
static Tmpl<T>& GlobalInstance()
{
static Tmpl<T> m_Singleton;
return m_Singleton;
};
};

function of class definition in source file v.s. inline functions [duplicate]

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.

C++ Connecting two .cpp files [duplicate]

This question already has answers here:
Why can templates only be implemented in the header file?
(17 answers)
Closed 9 years ago.
I have a problem connecting two .cpp files in C++. Here are my files
Header.h
//Header.h
template <class T> class asd{
asd();
check();
print();
}
file1.cpp
//file1.cpp
//defines all methods in class asd
#include "Header.h"
template<class T> asd<T>:: asd(T a, T b){//codes}
template<class T> T asd<T>:: check(T a){//codes}
template<class T> void asd<T>::print(){//codes}
file2.cpp
//file2.cpp
//main method
#include "Header.h"
int main(){//codes}
The thing I do not understand is that the code runs fine when I put main() inside file1.cpp, but it wont compile when I separate them into two files. Can someone please give pointers?
Edit:
For those with the same problem, solution can be found here:
http://www.cplusplus.com/forum/articles/14272/
Member functions of a class template should appear in the header file. Just move the function definitions from file1.cpp to Header.h.
Imagine you are the compiler. When compiling main, if you attempt to instantiate asd in any way, the compiler needs to be able to see the function definitions to generate the appropriate code. For example, if in main you do asd<int> my_asd;, the compiler needs to instantiate asd with T replaced with int. It can't do that for the functions if it can't see the function definitions.