Why does the rule apply only to identifiers and function arguments? Example:
Code: Select all
typedef char S[10];
S& f(void);
void g( const char * );
struct A { S m; };
A i(void);
void h(void) {
g("foo"); // Compliant with the letter of 5-2-12.
g(f()); // Compliant with the letter of 5-2-12.
g(i().m); // Compliant with the letter of 5-2-12.
}
Also the rationale says, "If a design requires arrays of different lengths, then a class should be used to encapsulate the array objects and so ensure that the dimensionality is maintained."
This question was motivated by a test case from one of our users:
Code: Select all
#include <string>
void dummy() {
std::string s;
s.assign("Hallo");
}
Code: Select all
#include <string>
void dummy() {
const char h[] = "Hallo";
std::string s;
s.assign(h);
}
What about the restriction to cases of function arguments? Why does this restriction not apply to other contexts where there is a decay from array to pointer type? (Think of initialization/assignment in the context of a return statement, in the definition of a variable, in a curly-braced initializer, or in a men-initializer.)
If this rule applies only to function arguments but not initializers, then there is a funny difference in the treatment of initializers: If a class object is initialized by a constructor that takes a pointer, then this rule applies when the initializer value is initially of array type (because a constructor is a function and the initializer value is the argument to that function). But if you change the type of the declared object to a bare pointer type, the rule does not apply.