Suppose I have the following two files:
test.cpp
inline double pi { 3.1415 };
#include <iostream>
void test() {
std::cout << pi << std::endl;
}
and
main.cpp
inline double pi { 2.178 };
void test();
#include <iostream>
int main() {
std::cout << pi << std::endl;
test();
}
inline
variables by default have external linkage. So, my initial thought, according to the One Definition Rule (ODR) is that the same variable can be declared across multiple translation units as long as the variable is declared inline and contains the same exact definition
So my first question is, why does this program compile if, even though the variables are declared inline, they have different definitions across the translation units?
Second, what is the difference between an inline non-const and an inline const variable?
For example, if you run the code above you should get the following output
2.178
2.178
However, if you go to both files and make pi
a const double
, that is inline const double pi {3.1415}
and inline const double pi {2.178}
You get the following output:
2.178
3.1415
What exactly is happening here?