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

标签 c++ c++11 standards one-definition-rule

<分区>

谁能解释 ISO N3242 §3.2 第 4 点中的这个陈述

与 ISO 标准 2003 相比,n3242 的新增部分:

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

谁能解释一下当前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

不止一种定义

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.

静态常量数据成员的定义

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.

关于c++ - 谁能解释一下当前C++0x标准草案的这一段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5622092/

相关文章:

c++ - 按值返回指针集合

c++ - 为什么这个公共(public)成员函数不能在类内声明的私有(private)结构成员上调用 decltype?

c++ - 需要帮助编写一个函数来给我一个数字的所有状态

c++ - 无法使用 std::map 推断 lambda 的返回类型

c++ - Microsoft _s 函数,它们现在是 C++ 标准的一部分吗?

c++ - fallocate 与 posix_fallocate

c++ - 为什么运行 C++ CGI 时控制台和浏览器的结果不同?

除非包装在 std::ref 中,否则 C++11 lambda 通过复制捕获

c++ - 是否存在与平台无关的C++ fork 过程(例如某些标准库)?如何使我的代码可移植?

c - 位移位和整数提升?