Default values for array arguments
Just playing around a 开发者_JAVA百科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 things simple, let's just use an array. Like so:
void experimentA(char a[3] = {'a', 'b', 'c'});
The compiler (LLVM GCC 4.2 with GNU99) complains "Expected expression". That is quite obtuse, but I was told by colleagues that this is happening because the 'value' I'm trying to assign is statically allocated, whereas the variable I'm trying to assign it to (a[3]
) is auto.
But I'm not completely sure if that's the case, since I'm able to do this:
void experimentB(char a[3] = "abc");
And the compiler merely warns me that string-literal to char* conversion is deprecated.
I don't understand how "abc" differs fundamentally from {'a','b','c'} in order to cause this discrepancy. Any insight is much appreciated!
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.
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).
"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.
"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.
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.
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();
}
精彩评论