An Example for Not Sufficient Linkage - c++

According to the Linkage Section of this link,
If a variable, function, or another entity with the same name is declared in several scopes, but does not have sufficient linkage, then several instances of the entity are generated.
Any simple example for the does not have sufficient linkage case?

Related

C++ singleton lazy initialization implementation and linkage seems conflict

C++ Singleton design pattern I come across this question and learned that there are two ways to implement the singleton pattern in c++.
1) allocate the single instance in heap and return it in the instance() call
2) return a static instance in the instance() call, this is also known as the lazy initialization implementation.
But I think the second, that is the lazy initialization implementation, is wrong due to following reasons.
Static object returned from the instance() call has internal linkage and will have unique copies in different translation unit. So if user modifies the singleton, it will not be reflected in any other translation unit.
But there are many statement that the second implementation is correct, am I missing something?
In the context of a method, the static keyword is not about linkage. It just affects the "storage class" of the defined variable. And for static local variables the standard explicitly states:
9.3.6 A static local variable in a member function always refers to the same object, whether or not the member function is inline.
So it doesn't matter at all whether you put the code in a header or cpp file.
Note that for free / non-member function it does indeed depend on the linkage of the function, as KerrekSB pointed out.
The linkage of the name of implementation object does not matter. What matters is the linkage of the name of the function you use to access the object, and that name has, of course, external linkage:
thing.h:
Thing & TheThing(); // external linkage
thing.cpp:
#include "thing.h"
Thing & TheThing() { static Thing impl; return impl; }
Every use of the name TheThing in the program refers to the same entity, namely the function defined (uniquely) in thing.cpp.
Remember, linkage is a property of names, not of objects.
You are wrong, because the singleton is defined in one single translation unit, the one that contains the definition of the function that returns it. That means that all translation units that wants to use the singleton ask it to the single one that actually defines it, and in the end all use the same object (as expected for a singleton pattern :-) ).

Does a type just have 'linkage' other than internal/external linkage?

3.5 [basic.link] paragraph 8 from N4567 says that
... A type is said to have linkage if and only if:
it is a class or enumeration type that is named (or has a name for linkage purposes (7.1.3)) and the name has linkage; or
...
which exclusively elaborates on linkage of types—one of the entities that a name can denote. However, it seems that there is no information in this paragraph whether the linkage that a type shall have is internal or external. Is it because there is no need to define it at all, or because it is already defined by other paragraphs?
The type either has linkage, or it doesn't. This paragraph is self-contained: the first half defines the notion of a type having linkage, and the second half describes restrictions on the use of types having no linkage. The notion of linkage of a type is not used anywhere else, as far as I can tell.

storage duration of functions, structs, classes, enums, and unions

If global variables and objects have static storage duration and external linkage?
do functions have static storage duration and external linkage as well?
what about structs and classes and enumerators(has external linkage i know)/unions?
I figured they have no storage duration and have no linkage,but then i thought that (global)functions have external linkage by default,but in a class they have class scope do they have internal linkage or?
Storage duration and linkage are unrelated concepts.
Functions do not have "storage duration", since functions do not reside in storage. Only objects have storage duration. Types do not reside in storage either, which is why types do not have storage duration.
It is not correct to say that classes have no linkage in general. Named classes declared in namespace scope have external linkage. Member functions of classes with external linkage also have external linkage. Classes declared locally and nameless classes have no linkage.
Functions don't officially have a storage duration, but in essence they're static (i.e., every function exists for the entire duration of the program). They have internal linkage if you define them static or inside of an anonymous namespace, otherwise external linkage.
Storage class applies to an object, not a type definition like a class, struct or union. It's fairly common to have two objects of the same class, one with static storage duration, and another with auto storage duration.
Likewise, you could create one object with internal linkage and another with external linkage:
T x;
static T y;
The same goes for linkage of classes:
class X { }; // external linkage
namespace {
class Y {}; // internal linkage
};

Static and anonymous namespace [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Unnamed/anonymous namespaces vs. static functions
Is this completely redundant, or may there be a reason to do this?
namespace {
static void f() {
...
}
}
It looks redundant to me -- either being declared static or being in an anonymous namespace means it has internal linkage.
§3.5/3:
A name having namespace scope (3.3.6) has internal linkage if it is the name of:
— a variable, function or function template that is explicitly declared static;
§3.5/4:
[...] An unnamed namespace or a namespace declared directly or indirectly within an unnamed namespace has internal linkage. [...] A name having namespace scope that has not
been given internal linkage above has the same linkage as the enclosing namespace if it is the name of
— a variable; or
— a function; or
So, as it is right now, it has internal linkage because it's explicitly declared static. If it wasn't explicitly declared static, it would have internal linkage because it's declared inside an unnamed namespace. Same effect either way.
Note that here I'm replying specifically about a function -- there are a few obscure cases where there's a difference when you're dealing with the name of a type (e.g., class/struct/union), but I don't know of any such thing that applies in the case of a function.
As far as what internal linkage really means, that's one of those places the standard is actually quite direct and clear. It's probably best to quote the definitions of all three possibilities (§3.5/2):
When a name has external linkage, the entity it denotes can be referred to by names from scopes of other translation units or from other scopes of the same translation unit.
When a name has internal linkage, the entity it denotes can be referred to by names from other scopes in the same translation unit.
When a name has no linkage, the entity it denotes cannot be referred to by names from other scopes.
Note that the italics above match those in the standard, which is its way of saying that these sentences define what those phrases mean throughout the rest of the standard.

Why do functions/objects inside anonymous namespace have external linkage?

Why don't symbols (functions and variables) that are defined in an anonymous namespace have internal linkage as with static keyword? If a function is not visible/accessible outside, what is the reason to have external linkage?
In C++03, names with internal linkage were forbidden from being used as template arguments[*]. So, names of most things in unnamed namespaces had external linkage to allow their use with templates. You could explicitly give a name internal linkage in an unnamed namespace by declaring it static, same as in a named or global namespace.
Both things changed in C++11 -- names in unnamed namespaces have internal linkage by default (3.5/4), and names with internal linkage can be used as template arguments.
[*] for types, it must have external linkage. For objects and functions, it must have external linkage if its address is used as a template argument, although it's OK for example to use as a template argument the value of a const integer with internal linkage.