3

I have a header file that contains the following definition

const std::string error[] = {"a", "b"}; 

Now I'm including this file in two different translation units and compiling the source codes. Everything works, but why? This is expected to break one definition rule.

Now even more interesting, I'm changing the type

const char* error[] = {"a", "b"}; 

and here it is, expected error

multiple definition of `error' 

It works the same way as for std::string for int, char, short and other integral types. What is this?

3 Answers 3

12

const gives namespace-scoped variables internal linkage, that's why it works, effectively the same as

static const std::string error[] = {"a", "b"}; 

The second one doesn't work because it's not the variable that is const, but the chars it consists of.

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

5 Comments

Didn't know that const gives an internal linkage. Thanks. Is it mentioned in standard?
Yes, found it. Objects declared const and not explicitly declared extern have internal linkage.
@axe What's the reference?
Never mind. It's 7.1.1 Storage class specifiers [dcl.stc], paragraph 7.
Also note that this is one of the few places where C++ breaks compatibility with C. See this question for details and rationale.
2

Global variables declared const have internal linkage, as if they were also declared static. You can define internal variables with the same name in different translation units, so that's what happens in your first example - each unit that includes the header gets its own copy of the array.

The second example is not const - the pointers point to constant objects, but are themselves mutable. So this array has external linkage, and is subject to the One Definition Rule.

Comments

0

From MSDN

In C, constant values default to external linkage, so they can appear only in source files. In C++, constant values default to internal linkage, which allows them to appear in header files.

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.