2 questions about storage-class specifiers

Hi there!

I have two questions about storage-class specifiers.

C++ Standard:
1) 7.1.1.6

A name declared in a namespace scope without a storage-class-specifier has external linkage unless it has internal linkage because of a previous declaration and provided it is not declared const. Objects declared const and not explicitly declared extern have internal linkage.


According to this, if we declare global constant as extern, it gets external linkage!

That's not true - a linker will show an error if we want to use that constant in another translation unit...

2) 7.1.1.8

The name of a declared but undefined class can be used in an extern declaration. Such a declaration can only be used in ways that do not require a complete class type. [Example:

1
2
3
4
5
6
7
8
9
struct S;
extern S a;
extern S f();
extern void g(S);
void h()
{
g(a); //error: S is incomplete
f(); //error: S is incomplete
}


—end example]


In my opinion, calling f function doesn't require a complete class type..
Calling g function does, because in this case the copying process have to occur.

Then why calling f is wrong ?
john891 wrote:
According to this, if we declare global constant as extern, it gets external linkage!

That's not true - a linker will show an error if we want to use that constant in another translation unit...


You are mistaken.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//file a.cpp

//global const variable declared as extern
extern const int x = 99;  //external linkage


//file b.cpp

extern const int x;

int main()
{
     cout << x << endl; //ok

     return 0;
}



On the second issue.
function f() returns a struct of type S - so we need the definition of S
Last edited on
1@

Sorry, my mistake :)

2@

But what if f is in another translation unit ? In my opinion the definition of S should be needed when f is defined in the same TU.
If you want to use a variable of typeX or a reference to a variable of typeX - then the definition
of typeX must be available to the compiler at that time.

This does not apply to creation of a pointer to typeX - a forward declaration will do in this case.
OK, I get it now. thx ;)
Topic archived. No new replies allowed.