6

According to clause 3 of section 3.5 of C++ 1998 standard, a const reference has internal linkage.

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.

But why multiple definition conflict is generated when compiling the following code?

// a.cpp const int& a = 1; int main() { return 0; } // b.cpp const int& a = 1; 

And then compile the code.

$ g++ a.cpp b.cpp /tmp/ccb5Qi0M.o:(.bss+0x0): multiple definition of `a' /tmp/ccD9vrzP.o:(.bss+0x0): first defined here collect2: error: ld returned 1 exit status 

If the const reference is changed to const, as follows

// a.cpp const int a = 1; int main() { return 0; } // b.cpp const int a = 1; 

It is OK to compile.

3 Answers 3

2

The reference itself isn't const, just the object it refers to; so (arguably) this rule doesn't give the reference internal linkage.

It doesn't make sense for a reference to be declared const. The C++11 standard clarifies the wording:

a variable that is explicitly declared const or constexpr and neither explicitly declared extern nor previously declared to have external linkage

with no mention of the nonsensical concept of references declared const.

Sign up to request clarification or add additional context in comments.

5 Comments

You mean the wording in the C++ 1998 standard is misleading?
@spockwang: I don't know about "misleading", but it's certainly confusing, and has now been clarified.
Does anyone have a good example for using such a reference, and wanting it not file-scoped by default?
@MikeSeymour Isn't the reference a variable in the C++11 standard?
@spockwang: Yes. But it can't be explicitly declared const, so this rule doesn't apply to it.
0

I think @Mike is correct, here is no new thing, just a little tip.

There is reference and the object that referenced to, the object can be constant then comes internal linkage, but the reference itself can never be constant since it has no CV concept(reference was initialized during declaration, then never shift to some object else, I remember GCC complains if you give a constant reference, mean for int const& r = o; although VS was not complaining, that just makes no sense), since the reference is neither constant nor static, then the declaration crosses source files.

Comments

0

This is a program with the most const reference ever. It compiles, and links with C++17 (try it):

// a.cpp extern const int& ir; int main() { return ir; } 
// b.cpp constexpr const int& ir = 42; 

As you can see, ir can be referenced from another translation unit, i.e. it has external linkage.

Conclusion: global reference variables have external linkage, unless declared as static or in anonymous namespace. Also the constexpr keyword on references does not make the reference a constant view to the referree (try it).


And constexpr references are not implicitly inline. This is redefinition of ir in C++17 (try it):

// a.cpp constexpr const int& ir = 42; int main() { return ir; } 
// b.cpp constexpr const int& ir = 42; 

If you put inline before the declaration of ir in both files, it compiles and links (try it).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.