2

Isn't the extern keyword supposed to simply 'blind' the compiler? Here are the codes that i can't understand why it is without an error.

struct A {
    int a;
};

class B {
    static A x;
public:
    void f() { x.a=0; }
};

extern A B::x; // not allocated.

main() {
    B z;
    z.f();
}

As you already know, the static member should be instantiated manually. However, i added the extern keyword, which means it's not actually allocated. It is very weird that it compiles fine!

  • 1
    I believe that the variable is allocated, it simply is not initialized, which is quite different. Although since you're using a class object, the default constructor may run. – Chris Hayes Sep 15 '13 at 02:22
  • @ChrisHayes How is that possible? Do you mean to say the extern keyword 'may' allocate a variable? –  Sep 15 '13 at 02:25
  • @rici I can't see nothing that even hints at `B::x` being in a different CU. Actually the only reason why this code compiles seems to be because it's all in one CU. – syam Sep 15 '13 at 02:26
  • @rici It's in a same compilation unit. Furthermore, I accessed the B::x by z.f(). –  Sep 15 '13 at 02:26
  • @syam you mean, if it's in the same compilation unit and the declaration is not found, the extern keyword may be omitted? –  Sep 15 '13 at 02:29
  • @isbae93: yeah, i saw that afterwards. You also left out a semicolon, so it doesn't actually compile :) (in that very line) – rici Sep 15 '13 at 02:29
  • @rici Oh, sorry. the semicolon is in right place in my source code. I should edit the post. Thank you. –  Sep 15 '13 at 02:31
  • cl.exe from vs2008: 1) missing return type in main. 2) extern not allowed on class members. 3) unresolved external. Sounds like a compiler quirk. – SigTerm Sep 15 '13 at 02:33
  • Omitting the `extern` line causes it to fail in the way you expect... – Adam Burry Sep 15 '13 at 02:33
  • Why don't you contact gcc devs? As far as I can tell, it shouldn't link, unless there's some small detail in C++ standard I never heard about. Could be a bug. – SigTerm Sep 15 '13 at 02:36
  • Thank you all of you. I should contact gcc devs like @SigTerm said. –  Sep 15 '13 at 02:38
  • Vaguely related question: http://stackoverflow.com/questions/7917500/are-static-class-variables-the-same-as-extern-variables-only-with-class-scope – Adam Burry Sep 15 '13 at 02:40
  • Sorry. It's g++ not gcc. Thank you again all of you. –  Sep 15 '13 at 02:44

1 Answers1

6

There is no such thing as an extern declaration for static member variables! The variable is declared extern anyway based on the definition of the class. gcc warns that you can't explicitly declare a static member variable to be extern. However, both gcc and clang compile and link the code without further ado, clearly ignoring the extern.

Of course, with the above code it is also clear that you are compiling with some non-standard mode in the first place as main() relies on the implicit int rule which was never part of C++.

Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
  • I've added -lstdc++. It just complied... How can i make the g++ more strict? –  Sep 15 '13 at 03:14
  • The `-lstdc++` option clearly has nothing to do with it: this is just explicitly adding the standard C++ library. You might want to compile with `-W -Wall -pedantic`. If you want to reject programs producing these warnings you'd also add `-Werror` or `-Werror=pedantic`. – Dietmar Kühl Sep 15 '13 at 03:19