#inline variable

37 messages · Page 1 of 1 (latest)

robust condorBOT
#

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.

weary kraken
#

!format

robust condorBOT
#

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.
Oppenheimer
weary kraken
#

!format

robust condorBOT
#

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.

Oppenheimer
weary kraken
#

!format

robust condorBOT
#

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.

Oppenheimer
weary kraken
robust condorBOT
#

@weary kraken

Please Do Not Delete Posts!

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.

weary kraken
weak citrus
#

uhm i would say the inline specifier is completely useless due to constexpr in this case

weary kraken
weak citrus
#

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

untold chasm
#

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

weak citrus
#

wdym

untold chasm
#

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

weak citrus
weary kraken
untold chasm
#

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

weary kraken
weak citrus
#

no offense but i think you should learn these terms before starting programming

#

specially when its a ""low level"" language such as c++

weary kraken
#

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

weak citrus
#

and well yes to use the inline variable you will need to include the header in every .cpp file

weary kraken
weak citrus
#

if you have global variables

#

then yes

#

you can also avoid this by using extern keyword

weary kraken
weary kraken
#

Thanks there both of you.

#

!solved