constexpr does not imply inline for non-static variables (C++17 inline variables)
While constexpr does imply inline for functions, it does not have that effect for non-static variables, considering C++17 inline variables.
For example, if you take the minimal example I posted at: How do inline variables work? and remove the inline, leaving just constexpr, then the variable gets multiple addresses, which is the main thing inline variables avoid.
constexpr static variables are however implicitly inline.
Minimal example that constexpr implies inline for functions
As mentioned at: https://stackoverflow.com/a/14391320/895245 the main effect of inline is not to inline but to allow multiple definitions of a function, standard quote at: How can a C++ header file include implementation?
We can observe that by playing with the following example:
main.cpp
#include <cassert>
#include "notmain.hpp"
int main() {
assert(shared_func() == notmain_func());
}
notmain.hpp
#ifndef NOTMAIN_HPP
#define NOTMAIN_HPP
inline int shared_func() { return 42; }
int notmain_func();
#endif
notmain.cpp
#include "notmain.hpp"
int notmain_func() {
return shared_func();
}
Compile and run:
g++ -c -ggdb3 -O0 -Wall -Wextra -std=c++11 -pedantic-errors -o 'notmain.o' 'notmain.cpp'
g++ -c -ggdb3 -O0 -Wall -Wextra -std=c++11 -pedantic-errors -o 'main.o' 'main.cpp'
g++ -ggdb3 -O0 -Wall -Wextra -std=c++11 -pedantic-errors -o 'main.out' notmain.o main.o
./main.out
If we remove inline from shared_func, link would fail with:
multiple definition of `shared_func()'
because the header gets included into multiple .cpp files.
But if we replace inline with constexpr, then it works again, because constexpr also implies inline.
GCC implements that by marking the symbols as weak on the ELF object files: How can a C++ header file include implementation?
Tested in GCC 8.3.0.