谁能解释一下当前 C++0x 标准草案的这一段吗?

发布于 2024-10-31 17:18:46 字数 523 浏览 2 评论 0原文

谁能解释一下 ISO N3242 §3.2 第 4 点中的这一说法

与 ISO 标准 2003 相比,n3242 的添加部分:

4 如果类的使用方式要求类类型完整,则翻译单元中只需要该类的一个定义。

类类型 T 必须是完整的,如果:

  • 声明了 T 类型的非静态类数据成员 (9.2),或者
  • T 用作 new 表达式中的对象类型或数组元素类型
  • 类型T是alignof表达式(5.3.6)的主题,或者
  • 异常声明具有类型 T、对 T 的引用或指向 T 的指针

任何人都可以解释当前 C++0x 标准草案的这一段吗?

在这些声明中添加此内容的实际含义是什么?

任何人都可以借助示例/程序来解释这一点吗?

Can anyone explain this statement from ISO N3242 §3.2, 4th point

The added part of n3242 when compare to ISO Standard 2003 :

4 Exactly one definition of a class is required in a translation unit if the class is used in a way that requires theclass type to be complete.

A class type T must be complete if:

  • a non-static class data member of type T is declared (9.2), or
  • T is used as the object type or array element type in a new-expression
  • the type T is the subject of an alignof expression (5.3.6), or
  • an exception-declaration has type T, reference to T, or pointer to T

Can anyone explain this paragraph of the current C++0x standard draft?

What is the actual meaning of adding this in these statement?

Can any one explain this with the help of an example/program?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

与之呼应 2024-11-07 17:18:46

直接来自维基百科

一般来说,一个翻译单元不应包含超过一个
任何类类型的定义。在这个
例如,类的两个定义
C型出现在同一个翻译中
单元。如果出现以下情况,通常会发生这种情况:
头文件被包含两次
相同的源文件,但没有适当的
标头防护装置。

class C {}; // first definition of C
class C {}; // error, second definition of C

n 下面,形成一个指向
S 或定义一个函数
对 S 的引用是法律的示例
构造,因为它们不
要求S的类型完整。
因此,定义不是
必填。

定义一个S类型的对象,a
采用 S 类型参数的函数,
或在 sizeof 表达式中使用 S 是
S 必须是的上下文示例
完整的,因此需要一个
定义。

struct S;   // declaration of S
S * p;      // ok, no definition required
void f(S&); // ok, no definition required
void f(S);  // ok, no definition required 
S f();      // ok, no definition required  

S s;        // error, definition required
sizeof(S);  // error, definition required

多个定义

在某些情况下,可能会有更多
比一个类型或一个的定义
模板。一个程序包括
多个头文件和源文件
通常会有多个
类型的定义,但仅此而已
每个翻译不止一个定义
单位。

如果一个程序包含多个
定义一个类型,然后每个
定义必须是等价的。

静态常量数据成员的定义

在准标准 C++ 中,所有静态数据
成员需要外部定义
他们班级的。然而,在此期间
C++标准化进程是
决定取消此要求
静态常量整型成员。这
目的是允许以下用途:

struct C
{
  static const int N = 10;
};
char data[C::N]; // N "used" without out-of-class definition

没有 N 的命名空间范围定义。

尽管如此,1998 年 C++ 标准的措辞仍然需要
定义如果该成员用于
程序。这包括
成员出现在任何地方,除了
sizeof 或 typeid 的操作数,
有效地实现上述
格式错误。

这被确定为缺陷,并调整措辞以允许
这样的成员出现在任何地方
需要常量表达式,
无需课外
定义。这包括数组
边界、case 表达式、静态
成员初始值设定项和非类型
模板参数。

struct C
{
  static const int N = 10;
  static const int U = N; // Legal per C++03
};

char data[C::N]; // Legal per C++03

template<int> struct D;

template<> struct D<C::N> {}; // Legal per C++03

但是,使用静态常量积分
任何地方的成员,除了
积分常量表达式是
需要定义

struct C
{
  static const int N = 10;
};

int main()
{
  int i = C::N; // ill-formed, definition of C::N required
}

这一要求将在
即将推出的 C++ 标准,
通俗地称为 C++0x。

Straight from Wikipedia:

In general, a translation unit shall contain no more than one
definition of any class type. In this
example, two definitions of the class
type C occur in the same translation
unit. This typically occurs if a
header file is included twice by the
same source file without appropriate
header guards.

class C {}; // first definition of C
class C {}; // error, second definition of C

n the following, forming a pointer to
S or defining a function taking a
reference to S are examples of legal
constructs, because they do not
require the type of S to be complete.
Therefore, a definition is not
required.

Defining an object of type S, a
function taking an argument of type S,
or using S in a sizeof expression are
examples of contexts where S must be
complete, and therefore require a
definition.

struct S;   // declaration of S
S * p;      // ok, no definition required
void f(S&); // ok, no definition required
void f(S);  // ok, no definition required 
S f();      // ok, no definition required  

S s;        // error, definition required
sizeof(S);  // error, definition required

More than one definition

In certain cases, there can be more
than one definition of a type or a
template. A program consisting of
multiple header files and source files
will typically have more than one
definition of a type, but not more
than one definition per translation
unit.

If a program contains more than one
definition of a type, then each
definition must be equivalent.

Definitions of static const data members

In pre-standard C++, all static data
members required a definition outside
of their class. However, during the
C++ standardization process it was
decided to lift this requirement for
static const integral members. The
intent was to allow uses such as:

struct C
{
  static const int N = 10;
};
char data[C::N]; // N "used" without out-of-class definition

without a namespace scope definition for N.

Nevertheless, the wording of the 1998 C++ standard still required a
definition if the member was used in
the program. This included the
member appearing anywhere except as
the operand to sizeof or typeid,
effectively making the above
ill-formed.

This was identified as a defect, and the wording was adjusted to allow
such a member to appear anywhere a
constant expression is required,
without requiring an out-of-class
definition. This includes array
bounds, case expressions, static
member initializers, and nontype
template arguments.

struct C
{
  static const int N = 10;
  static const int U = N; // Legal per C++03
};

char data[C::N]; // Legal per C++03

template<int> struct D;

template<> struct D<C::N> {}; // Legal per C++03

However, using a static const integral
member anywhere except where an
integral constant-expression is
required requires a definition

struct C
{
  static const int N = 10;
};

int main()
{
  int i = C::N; // ill-formed, definition of C::N required
}

This requirement will be relaxed in
the upcoming C++ standard,
colloquially referred to as C++0x.

素手挽清风 2024-11-07 17:18:46

它的意思是,如果您以需要定义的方式使用类类型(并且他们明确列出了使用类的方法),则必须提供此类类的一个定义。

如果您不提供定义,则会出现错误。如果您在一个翻译单元中提供多个内容,则会出现错误。如果您在多个翻译单元中提供多个定义,则这是未定义的行为。

All it's saying is that if you make use of a class type (and they're explicitly listing ways to use a class) in a way that requires a definition, you must provide exactly one definition of such class.

If you don't provide a definition it's an error. If you provide more than one in a translation unit it's an error. If you provide more than one definition across multiple translation units it's undefined behavior.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文