Reputation: 21
Is this, line of code that is presented below and under this sentence, considered a declaration or definition?
extern const int &ri;
Upvotes: 2
Views: 430
Reputation:
Apart from anything else, it is almost certainly a misuse of a reference. References are intended to be used as function parameters and and return values. THe fact that you can create "reference variables" (I don't know how else to describe them) seems to have been an accident of the C++ grammar. Yes, you can do tricks with them, but no, you shouldn't.
Bottom line - if you have a reference that isn't a parameter or a return value, you are probably doing something wrong. Unless you really, really know what you are doing, in which case you wouldn't be asking about it here, would you?
Upvotes: 1
Reputation: 320777
Firstly, declaration and definition are not mutually exclusive concepts. Every definition is a declaration at the same time (with few exceptions). Which means that the proper way to ask the question would be: "Is this a defining declaration or a non-defining declaration?".
Note that I'm not saying that the question the way it is now is meaningless. The question is fine, because everybody understands what is really meant by it. I just wanted to say this as an introductory note.
Secondly, what you have here is a non-defining declaration, meaning that this declaration is not a definition (a reference or object declaration with extern
specifier but without an initializer is not a definition).
In case of
extern const int &ri = 5;
we'd also have a declaration, but this would be a defining declaration (i.e. a declaration that happens to be a definition).
Upvotes: 3
Reputation: 361812
extern const int &ri;
Its a declaration ONLY with no definition, which indicates that the variable is defined elsewhere.
Such code is usually found in .h
file, and its definition is found in .cpp
file. This approach is used to avoid multiple-redefinition error if you include the header file in multiple files. Something like this:
//lib.h
extern Type object; //declaration Only;
//lib.cpp
#include "lib.h"
Type object = /*some initialization - optional*/; //definition
//A.cpp
#include "lib.h"
Type oA = object;
//B.cpp
#include "lib.h"
Type oB = object;
The usage of const
at namespace level makes the variable being declared or defined to have internal-linkage, it becomes like an immutable static
variable, which exists only in its own translation unit.
extern const
makes the variable to have external-linkage, at the same time, the variable is constant.
namespace N
{
const int i = 10; //i has internal linkage
extern const int j = 10; //j has external linkage
}
In this case, extern
is used to make the variable to have external-linkage!
§3.5/3 [basic.link]:
A name having namespace scope (3.3.5) has internal linkage if it is the name of
— an object, reference, function or function template that is explicitly declared static or,
— an object or reference that is explicitly declared const and neither explicitly declared extern nor previously declared to have external linkage; or
— a data member of an anonymous union.
Upvotes: 1
Reputation: 168876
C++03, §3.1, ¶2:
A declaration is a definition unless ... it contains the extern specifier or a linkage- specification and neither an initializer nor a function-body ...
So, extern const int &ri;
, which contains the extern
specifier and not an initializer is a declaration but not a definition.
Upvotes: 3
Reputation: 92391
Using extern
would indicate that the object is not defined here, but somewhere else.
Upvotes: 0
Reputation: 147056
It's a declaration- the "extern" means that it lives somewhere else.
Upvotes: 4