Function-Local Static Const Parameter Semantics initialization
The questions are in bold, for those that can not be difficult to read the question in depth.
This is a question for this question. It has to do with the semantics of initializing static variables in functions. Static variables need to be initialized once, and their internal state can be changed later - as I ( currently ) do in the linked question. However, this code does not require the function to change the state of the variable later.
Let me clarify my position since I don't need to change the internal state of the string object. The code is for a trait class for metaprogramming, and as such would be useful from const char * const ptr - thus, ideally a static constant local value variable is needed. My educated guess is that in this case the corresponding string will be optimally allocated in memory by the linkloader and that the code is safer and matches the intended semantics.
This leads to the semantics of such a variable "The Third Edition C ++ Programming Language - Stroustrup" has nothing (which I can find) to say about it. All that is said is that the variable is initialized once, when the flow of control first reaches the code. This makes me wonder if the following code is needed, and if not the intended semantics?
#include <iostream>
const char * const GetString(const char * x_in)
{
static const char * const x = x_in;
return x;
}
int main()
{
const char * const temp = GetString("yahoo");
std::cout << temp << std::endl;
const char * const temp2 = GetString("yahoo2");
std::cout << temp2 << std::endl;
}
The following compilations are on GCC and prints "yahoo" twice. This is what I want - however it may not be standards compliant (which is why I am posting this question). Perhaps it would be more elegant to have two functions, "SetString" and "String", where the latter goes to the first. If the standard is compatible, does anyone know about template injection in boost (or elsewhere)?
edit: 11-May-2010
I am using the following macro to generate the aforementioned getter / setters in my classes that encode compile time information.
#define MACRO_STATIC_SETTING_PTR(name, type) \
static const type const set_##name (const type const name##_in) { \
static const type const name = name##_in; \
return name; \
} \
static const type const name() { \
return set_##name(NULL); \
}
#define MACRO_STATIC_SETTING(name, type) \
static const type set_##name (const type name##_in) { \
static const type name = name##_in; \
return name; \
} \
static const type name() { \
return set_##name(NULL); \
}
These macros are placed inside the class declaration. For instance:
template<class tag>
class ConfigInstance{
public:
MACRO_STATIC_SETTING_PTR(sqllite3_filename, char *)
};
Hope this will be helpful to others.
a source to share
Let's take a look at part 6.7 §4 (declaration statement) of the C ++ standard.
An implementation is allowed to early initialize other local objects with static storage duration, under the same conditions that an implementation is allowed to statically initialize an object with static storage duration in the namespace scope. Otherwise, such an object is initialized with the first test pass through its expression; such an object is considered initialized by the completion of its initialization.
So we have two cases:
- Or we now, before introducing the function with static local
x
, what value does it matterx
. The compiler is then allowed to initialize the value as early as possible (even at compile time). - Now we don't get the value, we
x
initialize it only the first time we enter our function.
So, yes, what gcc does not only do what you want, this behavior is also standard compatible.
a source to share