Just playing around a little with C++. What I really want to do is to be able to setup a function with default values defined for an array or pointer argument. To keep thing
"abc"
is an expression, {'a', 'b', 'c'}
is a static initializer. The later is only allowed in variable declarations. For reasons unknown to me argument with default value has different grammar rule that does not allow static initializers.
There are some significant changes to when static initializers are allowed in C++0x, but I am not sure how it affect the case in question.
Your colleagues are wrong or maybe you misunderstood.
The first clue to understanding is that you cannot have an array as a function parameter in C or C++. The reasons are historical. So when you write void experimentA(char a[3] ...)
the compiler automatically converts it to a pointer, i.e. void experimentA(char* a ...)
. So the real question is why "abc"
is a suitable default value for a and { 'a', 'b', 'c' }
is not. The reason is as the compiler explains, "abc"
is an expression and { 'a', 'b', 'c' }
is not (its an initialiser). There are some places in C++ where you can use an initialiser and some where you can't. Default value for a parameter just happens to be one of the places you can't.
"abc"
is an expression. It happens to behave specially, different from all other expressions, when it is used to initialize a variable of type array of char
.
{'a','b','c'}
is not an expression but an initializer. It is only syntactically allowed in variable definitions. There, the syntax allows either an expression or a non-expression initializer, but that does not man that initializers can be used as expressions anywhere else.
When you use string literal "abc", it's allocated by compiler somewhere in the memory, and a pointer to its first character is used as default value. So for the compiler the code is like that: void experimentA(char a[3] = 0x12345678);
.
For the second case, the array literal is not allocated by a compiler as a string (which I would see as some inconsistency in the language).
One simple way to do this is via plain old function overloading. For example, below simulates the default parameter value for format parameter which is char* type:
static string to_string(time_point<system_clock> time)
{
return to_string(time, "%Y-%m-%d-%H-%M-%S");
}
static string to_string(time_point<system_clock> time, const char* format)
{
time_t tt = system_clock::to_time_t(time);
char str[1024];
if (std::strftime(str, sizeof(str), format, std::localtime(&tt)))
return string(str);
else return string();
}
A default parameter must be valid.
You can call
f("abc")
but never
f({'a','b','c'});
"abc" is effectively an address in memory and {'a','b','c'} means initialise an array or struct/class.