I have a val.h
source code file with the global variable with internal linkage and inline function that returns its address:
// val.h
#pragma once
static int val;
inline int* get_val()
{
return &val;
}
Then this header is included in two different translation units. If I call &val
in both of these units, I get two different addresses, and it's ok since val
has an internal linkage and each translation unit has its own val
. But if I call get_val()
in both of these units, I get two equal addresses.
Is such behavior guaranteed by the standard, would we always obtain the same value returned by get_val()
call from any translation unit?
And what if compiler decides to make true inlining, i.e. just to replace get_val()
call by &val
statement in each translation unit. Would we get different addresses for each translation unit in such case?
Your function violates the One Definition Rule since the expression val
refers to different entities in different translation units.
Its behaviour is undefined.