为什么 class 数据成员不能用直接初始化语法初始化?
Why class data members can't be initialized by direct initialization syntax?
我很好奇为什么 class' 的数据成员不能用 () 语法初始化?考虑以下示例:
#include <iostream>
class test
{
public:
void fun()
{
int a(3);
std::cout<<a<<'\n';
}
private:
int s(3); // Compiler error why???
};
int main()
{
test t;
t.fun();
return 0;
}
程序编译失败并给出以下错误。
11 9 [Error] expected identifier before numeric constant
11 9 [Error] expected ',' or '...' before numeric constant
为什么?是什么原因? C++ 标准对 class 数据成员的初始化有何规定?
非常感谢您的帮助。谢谢
Early proposals leading to the feature's introduction explain that this is to avoid parsing problems.
这里只是其中的示例之一:
Unfortunately, this makes initializers of the “(
expression-list )
” form ambiguous at the time that the declaration is being parsed:
struct S {
int i(x); // data member with initializer
// ...
static int x;
};
struct T {
int i(x); // member function declaration
// ...
typedef int x;
};
One possible solution is to rely on the existing rule that, if a declaration could be an object or a function, then it’s a function:
struct S {
int i(j); // ill-formed...parsed as a member function,
// type j looked up but not found
// ...
static int j;
};
A similar solution would be to apply another existing rule, currently used only in templates, that if T
could be a type or something else, then it’s something else; and we can use “typename
” if we really mean a type:
struct S {
int i(x); // unabmiguously a data member
int j(typename y); // unabmiguously a member function
};
Both of those solutions introduce subtleties that are likely to be misunderstood by many users (as evidenced by the many questions on comp.lang.c++ about why “int i();
” at block scope doesn’t declare a default-initialized int
).
The solution proposed in this paper is to allow only initializers of the “=
initializer-clause” and “{
initializer-list }
” forms. That solves the ambiguity problem in most cases. [..]
我很好奇为什么 class' 的数据成员不能用 () 语法初始化?考虑以下示例:
#include <iostream>
class test
{
public:
void fun()
{
int a(3);
std::cout<<a<<'\n';
}
private:
int s(3); // Compiler error why???
};
int main()
{
test t;
t.fun();
return 0;
}
程序编译失败并给出以下错误。
11 9 [Error] expected identifier before numeric constant
11 9 [Error] expected ',' or '...' before numeric constant
为什么?是什么原因? C++ 标准对 class 数据成员的初始化有何规定? 非常感谢您的帮助。谢谢
Early proposals leading to the feature's introduction explain that this is to avoid parsing problems.
这里只是其中的示例之一:
Unfortunately, this makes initializers of the “
(
expression-list)
” form ambiguous at the time that the declaration is being parsed:struct S { int i(x); // data member with initializer // ... static int x; }; struct T { int i(x); // member function declaration // ... typedef int x; };
One possible solution is to rely on the existing rule that, if a declaration could be an object or a function, then it’s a function:
struct S { int i(j); // ill-formed...parsed as a member function, // type j looked up but not found // ... static int j; };
A similar solution would be to apply another existing rule, currently used only in templates, that if
T
could be a type or something else, then it’s something else; and we can use “typename
” if we really mean a type:struct S { int i(x); // unabmiguously a data member int j(typename y); // unabmiguously a member function };
Both of those solutions introduce subtleties that are likely to be misunderstood by many users (as evidenced by the many questions on comp.lang.c++ about why “
int i();
” at block scope doesn’t declare a default-initializedint
).The solution proposed in this paper is to allow only initializers of the “
=
initializer-clause” and “{
initializer-list}
” forms. That solves the ambiguity problem in most cases. [..]