能在没有长度的情况下声明一个静态数组,然后用长度定义它吗?

huangapple go评论72阅读模式
英文:

Is it valid to declare a static array with no length, and then define it with a length?

问题

请考虑这段代码:

class Foo {
   private:
      static char buffer[];
};

char Foo::buffer[100];

这合法吗,还是我需要在类定义中也加入 buffer[100]

英文:

Consider this code:

class Foo {
   private:
      static char buffer[];
};

char Foo::buffer[100];

Is this valid, or do I need to put buffer[100] also in the class definition?

答案1

得分: 3

A non-inline static data member declared in a class may have an incomplete type.

从C++ 17标准(12.2.3.2静态数据成员)

2 **在其类定义中声明非内联静态数据成员不是定义,可以是除cv void之外的不完全类型。**未在类定义中定义为内联的静态数据成员的定义应出现在包含成员类定义的命名空间范围内。在命名空间范围的定义中,静态数据成员的名称应通过其类名使用::操作符进行限定。静态数据成员的初始化表达式在其类的作用域内(6.3.7)。

因此,在类定义中出现的这个声明(其中有一个拼写错误:类型指定了char出现两次)

static char char buffer[];

不是内联数据成员的定义,它可能具有不完全的字符数组类型。

另一方面,如果要在类定义内引用该数组,最好将其声明为完全类型。例如,这个类定义

class Foo
{
public:
void f() const
{
std::cout << std::size( buffer ) << '\n';
}
private:
static char buffer[];
};

将不会编译,因为在函数f中使用了不完全的数组类型表达式std::size( buffer )

英文:

A non-inline static data member declared in a class may have an incomplete type.

From the C++ 17 Standard (12.2.3.2 Static data members)

> 2 The declaration of a non-inline static data member in its class
> definition is not a definition and may be of an incomplete type other
> than cv void.
The definition for a static data member that is not
> defined inline in the class definition shall appear in a namespace
> scope enclosing the member’s class definition. In the definition at
> namespace scope, the name of the static data member shall be qualified
> by its class name using the :: operator. The initializer expression in
> the definition of a static data member is in the scope of its class
> (6.3.7).

So as this declaration (where there is a typo: the type specifies char is present twice) in the class definition

 static char char buffer[];

is not a definition (of an inline data member) it may have an incomplete character array type.

On the other hand, if you want to refer to the array within the class definition it is better to declare it as a complete type. For example this class definition

class Foo
{
public:
	void f() const
	{
		std::cout &lt;&lt; std::size( buffer ) &lt;&lt; &#39;\n&#39;;
	}
private:
	static char buffer[];
};

will not compile because within the function f in this expression std::size( buffer ) there is used an incomplete array type.

答案2

得分: 1

是的,这是有效的,因为对于 buffer,你在类内部有一个 声明,而不是一个定义。这可以从 静态成员的文档 中看出:

类体内的声明不是定义,可以声明成员为不完整类型(除了 void),包括在其中声明该成员的类型。

这意味着 buffer 在类内部的 声明 中可以具有不完整类型,比如 char[]

英文:

> Is this valid

Yes this is valid since for buffer you have a declaration that is not a definition inside the class. This can be seen from static member's documentation:

> The declaration inside the class body is not a definition and may declare the member to be of incomplete type (other than void), including the type in which the member is declared.

This means that buffer can have incomplete type such as char[] in its declaration inside the class.

huangapple
  • 本文由 发表于 2023年4月16日 23:30:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76028660.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定