Reputation: 28416
Several times I've seen code like this in a header
IMPORTOREXPORT std::string const& foo();
IMPORTOREXPORT std::string const& bar();
IMPORTOREXPORT std::string const& baz();
and the following corresponding code in a cpp file
std::string const& foo() {
static std::string const s{"foo"};
return s;
}
std::string const& bar() {
static std::string const s{"bar"};
return s;
}
std::string const& baz() {
static std::string const s{"baz"};
return s;
}
where IMPORTOREXPORT
is a macro to deal with linkage, so yes, I'm talking of a multi-module code, where different translation units are compiled independently and then linked together.
Is there any reason why I should prefer the above solution to simply have these in the header?
inline std::string const foo = "foo";
inline std::string const bar = "bar";
inline std::string const baz = "baz";
I found this related question, but I'm not sure it entirely answers this. For instance, the accepted answer contains this (part of a) statement
for different modules would be different addresses
but this doesn't seem to be inline with what read on cppreference (emphasis mine):
An inline function or variable (since C++17) with external linkage (e.g. not declared
static
) has the following additional properties:
- It has the same address in every translation unit.
Upvotes: 2
Views: 380
Reputation: 119174
As you note, the inline variable approach guarantees that the variable has a unique address, just like the static local variable approach.
The inline approach is certainly easier to write, but:
Upvotes: 1