10 KiB
[basic.def]
6 Basics [basic]
6.2 Declarations and definitions [basic.def]
A declaration ([basic.pre]) may (re)introduce one or more names and/or entities into a translation unit.
If so, the declaration specifies the interpretation and semantic properties of these names.
A declaration of an entity X is a redeclaration of X if another declaration of X is reachable from it ([module.reach]); otherwise, it is a first declaration.
A declaration may also have effects including:
a static assertion ([dcl.pre]),
controlling template instantiation ([temp.explicit]),
guiding template argument deduction for constructors ([temp.deduct.guide]),
use of attributes, and
nothing (in the case of an empty-declaration).
Each entity declared by a declaration is also defined by that declaration unless:
it declares a function without specifying the function's body ([dcl.fct.def]),
it contains theextern specifier ([dcl.stc]) or alinkage-specification15 ([dcl.link]) and neither an initializer nor afunction-body,
it declares a non-inline static data member in a class definition ([class.mem], [class.static]),
it declares a static data member outside a class definition and the variable was defined within the class with the constexpr specifier ([class.static.data]) (this usage is deprecated; see [depr.static.constexpr]),
it is an elaborated-type-specifier ([class.name]),
it is anopaque-enum-declaration ([dcl.enum]),
it is atemplate-parameter ([temp.param]),
it is aparameter-declaration ([dcl.fct]) in a functiondeclarator that is not the declarator of afunction-definition,
it is atypedef declaration ([dcl.typedef]),
it is an alias-declaration ([dcl.typedef]),
it is a namespace-alias-definition ([namespace.alias]),
it is a using-declaration ([namespace.udecl]),
it is a deduction-guide ([temp.deduct.guide]),
it is a static_assert-declaration ([dcl.pre]),
it is a consteval-block-declaration,
it is anattribute-declaration ([dcl.pre]),
it is anempty-declaration ([dcl.pre]),
it is a using-directive ([namespace.udir]),
it is a using-enum-declaration ([enum.udecl]),
it is a template-declaration ([temp.pre]) whose template-head is not followed by either a concept-definition or a declaration that defines a function, a class, a variable, or a static data member,
it is an explicit instantiation declaration ([temp.explicit]), or
it is an explicit specialization whosedeclaration is not a definition.
A declaration is said to be a definition of each entity that it defines.
[Example 1:
All but one of the following are definitions:int a; // defines aextern const int c = 1; // defines cint f(int x) { return x+a; } // defines f and defines xstruct S { int a; int b; }; // defines S, S::a, and S::bstruct X { // defines Xint x; // defines non-static data member xstatic int y; // declares static data member y X(): x(0) { } // defines a constructor of X};int X::y = 1; // defines X::yenum { up, down }; // defines up and downnamespace N { int d; } // defines N and N::d X anX; // defines anX whereas these are just declarations:extern int a; // declares aextern const int c; // declares cint f(int); // declares fstruct S; // declares Stypedef int Int; // declares Intnamespace N1 = N; // declares N1extern X anotherX; // declares anotherXusing N::d; // declares d
â end example]
[Note 1:
In some circumstances, C++ implementations implicitly define the default constructor ([class.default.ctor]), copy constructor, move constructor ([class.copy.ctor]), copy assignment operator, move assignment operator ([class.copy.assign]), or destructor member functions.
â end note]
[Example 2:
Given#include struct C { std::string s; // std::string is the standard library class ([string.classes])};
int main() { C a; C b = a; b = a;} the implementation will implicitly define functions to make the definition of C equivalent tostruct C { std::string s; C() : s() { } C(const C& x): s(x.s) { } C(C&& x): s(static_caststd::string&&(x.s)) { }// : s(std::move(x.s)) { } C& operator=(const C& x) { s = x.s; return this; } C& operator=(C&& x) { s = static_caststd::string&&(x.s); return this; }// { s = std::move(x.s); return *this; }~C() { }};
â end example]
[Note 2:
A class name can also be implicitly declared by anelaborated-type-specifier ([dcl.type.elab]).
â end note]
In the definition of an object, the type of that object shall not be an incomplete type ([basic.types.general]), an abstract class type ([class.abstract]), or a (possibly multidimensional) array thereof.
Appearing inside the brace-encloseddeclaration-seq in a linkage-specification does not affect whether a declaration is a definition.