Default values for array arguments

余生颓废 提交于 2019-11-30 18:22:23

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();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!