我不明白两者的意义。


当前回答

从C99标准,6.7(5):

声明指定一组标识符的解释和属性。标识符的定义是该标识符的声明:

对于一个对象,使存储空间为该对象保留; 对于函数,包括函数体; 类的(唯一)声明 标识符。

来自c++标准3.1(2):

声明是定义,除非它声明了一个函数而没有指定函数体,它包含extern说明符或链接规范,但既没有初始化式也没有函数体,它在类声明中声明了静态数据成员,它是类名声明,或者它是typedef声明,using-declaration,或using-directive。

下面是一些例子。

有趣的是(也许不是,但我有点惊讶)typedef int myint;是C99中的定义,但在c++中只是声明。

其他回答

你不能用最一般的术语说,声明是一个没有分配存储空间的标识符,而定义实际上是从声明的标识符分配存储空间吗?

一个有趣的想法是:只有类或函数与类型信息链接起来,模板才能分配存储空间。那么模板标识符是声明还是定义呢?它应该是一个声明,因为没有分配存储空间,您只是在对模板类或函数进行“原型化”。

从C99标准,6.7(5):

声明指定一组标识符的解释和属性。标识符的定义是该标识符的声明:

对于一个对象,使存储空间为该对象保留; 对于函数,包括函数体; 类的(唯一)声明 标识符。

来自c++标准3.1(2):

声明是定义,除非它声明了一个函数而没有指定函数体,它包含extern说明符或链接规范,但既没有初始化式也没有函数体,它在类声明中声明了静态数据成员,它是类名声明,或者它是typedef声明,using-declaration,或using-directive。

下面是一些例子。

有趣的是(也许不是,但我有点惊讶)typedef int myint;是C99中的定义,但在c++中只是声明。

从c++标准文档中添加定义和声明示例(来自3.1节声明和定义)

定义:

int a;                       // defines a
extern const int c = 1;      // defines c
int f(int x) { return x+a; } // defines f and defines x
struct S { int a; int b; };  // defines S, S::a, and S::b
struct X {                   // defines X
    int x;                   // defines non-static data member x
    static int y;            // DECLARES static data member y
    X(): x(0) { }            // defines a constructor of X
};
int X::y = 1;                // defines X::y
enum { up, down };           // defines up and down
namespace N { int d; }       // defines N and N::d
namespace N1 = N;            // defines N1
X anX;                       // defines anX

声明:

extern int a;                 // declares a
extern const int c;           // declares c
int f(int);                   // declares f
struct S;                     // declares S
typedef int Int;              // declares Int
extern X anotherX;            // declares anotherX
using N::d;                   // declares d

c++标准3.1节:

声明将名称引入到翻译单元中,或者重新声明前一个翻译单元中引入的名称 声明。声明指定这些名称的解释和属性。

下一段声明(强调是我的)声明是一种定义,除非……

... 它声明一个函数而不指定函数体:

void sqrt(double);  // declares sqrt

... 它在类定义中声明一个静态成员:

struct X
{
    int a;         // defines a
    static int b;  // declares b
};

... 它声明了一个类名:

class Y;

... 它包含没有初始化式或函数体的extern关键字:

extern const int i = 0;  // defines i
extern int j;  // declares j
extern "C"
{
    void foo();  // declares foo
}

... Or是类型定义或using语句。

typedef long LONG_32;  // declares LONG_32
using namespace std;   // declares std

现在,理解声明和定义之间的区别很重要的一个重要原因是:一个定义规则。c++标准第3.2.1节:

任何翻译单元都不能包含任何变量、函数、类类型、枚举类型或模板的多个定义。

c++ 11更新

由于我没有看到与c++ 11相关的答案,这里有一个。

声明是定义,除非声明了/n:

opaque enum - enum X: int; 模板参数-模板参数- MyArray 参数声明- x和y在int add(int x, int y); 别名声明-使用IntVector = std::vector<int>; - static_assert(sizeof(int) == 4, "Yikes!") 属性声明(实现定义的) 空声明;

以上列表从c++ 03继承的附加子句:

函数声明- add in int add(int x, int y); Extern说明符包含声明或链接说明符- Extern int a;或extern "C"{…}; 类中的静态数据成员-类C中的x{静态int x;}; 类/struct声明- struct Point; typedef int int; 使用声明-使用std::cout; 使用命名空间NS;

模板声明是一种声明。如果模板声明定义了函数、类或静态数据成员,那么模板声明也是定义。

以下例子来自于区分声明和定义的标准,我发现这些例子有助于理解它们之间的细微差别:

// except one all these are definitions
int a;                                  // defines a
extern const int c = 1;                 // defines c
int f(int x) { return x + a; }          // defines f and defines x
struct S { int a; int b; };             // defines S, S::a, and S::b
struct X {                              // defines X
    int x;                              // defines non-static data member x
    static int y;                       // DECLARES static data member y
    X(): x(0) { }                       // defines a constructor of X
};
int X::y = 1;                           // defines X::y
enum { up , down };                     // defines up and down
namespace N { int d; }                  // defines N and N::d
namespace N1 = N;                       // defines N1
X anX;                                  // defines anX


// all these are declarations
extern int a;                           // declares a
extern const int c;                     // declares c
int f(int);                             // declares f
struct S;                               // declares S
typedef int Int;                        // declares Int
extern X anotherX;                      // declares anotherX
using N::d;                             // declares N::d


// specific to C++11 - these are not from the standard
enum X : int;                           // declares X with int as the underlying type
using IntVector = std::vector<int>;     // declares IntVector as an alias to std::vector<int>
static_assert(X::y == 1, "Oops!");      // declares a static_assert which can render the program ill-formed or have no effect like an empty declaration, depending on the result of expr
template <class T> class C;             // declares template class C
;                                       // declares nothing