When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.
37 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.
!format
Ohkay but my question is ```cpp
inline header file and whether its shared without the include header file or
not in other code files
#ifndef CONSTANTS_H #define CONSTANTS_H // define your own namespace to hold
// constants namespace constants {
// inline constexpr double pi { 3.14159
// }; // note: now inline constexpr
// inline constexpr double avogadro
// { 6.0221413e23 }; inline constexpr
// double myGravity { 9.2 }; // m/s^2
// -- gravity is light on this planet
// // ... other related constants }
// #endif
COPY
main.cpp:
#include "constants.h" #include < iostream> int main(){std::cout << "Enter a radius: "; int radius{}; std::cin>> radius; std::cout << "The circumference is: " << 2.0 * radius * constants::pi << '\n'; return 0; }
We can include constants.h into as many code files as we want, but these variables will only be instantiated once and shared across all code files.
!format
Constants.cpp
#ifndef CONSTANTS_H
#define CONSTANTS_H
// define your own namespace to hold constants
namespace constants {
inline constexpr double pi{3.14159}; // note: now inline constexpr
inline constexpr double avogadro{6.0221413e23};
inline constexpr double myGravity{
9.2}; // m/s^2 -- gravity is light on this planet
// ... other related constants
} // namespace constants
#endif
main.cpp :
#include "constants.h"
#include <iostream>
int
main() {
std::cout << "Enter a radius: ";
int radius{};
std::cin >> radius;
std::cout << "The circumference is: " << 2.0 * radius * constants::pi << '\n';
return 0;
}
We can include constants.h into as many code files as we want, but these variables will only be instantiated once and shared across all code files.
!format
constants.h
#ifndef CONSTANTS_H
#define CONSTANTS_H
// define your own namespace to hold constants
namespace constants {
inline constexpr double pi{3.14159}; // note: now inline constexpr
inline constexpr double avogadro{6.0221413e23};
inline constexpr double myGravity{
9.2}; // m/s^2 -- gravity is light on this planet
// ... other related constants
} // namespace constants
#endif
main.cpp :
#include "constants.h"
#include <iostream>
int
main() {
std::cout << "Enter a radius: ";
int radius{};
std::cin >> radius;
std::cout << "The circumference is: " << 2.0 * radius * constants::pi << '\n';
return 0;
}
We can include constants.h into as many code files as we want, but these variables will only be instantiated once and shared across all code files.
My question is since we used inline thing in constant.h and included the constant.h header file in main.cpp .
They are saying "but they will only be instantiated once and shared across all code files:
So I dont understand English is not my first language
Are they (implying) that we don't have to include the header file constants.h?
@weary kraken
Please don't delete forum posts. They can be helpful to refer to later and other members can learn from them. In the future you can use !solved to close a post and mark a post as solved.
Sorry for posting a lot refer to this one
And here's my question
uhm i would say the inline specifier is completely useless due to constexpr in this case
@weak citrus Yeah but I didn't ask that ,you just opened more concerns LMAO ,let's stick to what I asked politely speaking.
i just said that in this case it would be useless
but answering the question, inline variable means that only one initialization of the variable will exist. all object files will reference the same memory address
Basically if ypu have one header with an inline x varible defined with 5, and then have another inpine x varaible in another file defined with 3, this is undefined behaviour
wdym
The inline specifier marks a variable/function as a weak reference, which means when the linker is resolving reference names, duplicate definitions don't cause a linker failure and instead will discard all of one but the definitions
What allows for inlining is that by being in the header file, is that the value of a reference is known at compile time for any translation unit which includes it
maybe with a simple example you understand it better notice how the inline addresses matches but the statics don't, when inline both .cpp files use the same variable (memory address)
https://godbolt.org/z/3qaehqPoE
Then use const to prevent any changes right?
The point is that in my example you have two different header files both independently defining an inline variable called x.
Const can't do anything because when compiling the first header file, the compiler cannot see the second and vice-versa
@weak citrus Please edit "same memory address" with something synonymously simple.
no offense but i think you should learn these terms before starting programming
specially when its a ""low level"" language such as c++
I only understand thus part
"but answering the question, inline variable means that only one initialization of the variable will exist."
This second one down here makes no sense to me
"all object files will reference the same memory "
If I answered my own question is i would have said
" in order for the variables to be shared across code files you hve to include the header file in all of the code files or not."
I wanted an answer along this lines@weak citrus
"all object files will reference the same memory "
variables are stored in memory addresses
and well yes to use the inline variable you will need to include the header in every .cpp file
I know
Not just an inline variable ,any variable right?
if you have global variables
then yes
you can also avoid this by using extern keyword
Cool lol believe or not I just wanted this clarification all this time lol.