0

main.cpp:

class A
{
    public:
        static const int x = 1;
};

int main()
{
    auto p = &A::x;
}

Compilation command line:

clang++ -std=c++20 -pedantic-errors main.cpp -o prog

Output:

/usr/bin/ld: /tmp/main-008420.o: in function `main':
main.cpp:(.text+0x8): undefined reference to `A::x'
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Why do I get that linker error? What part of the c++ standard forbids me to do this?

Supremum
  • 542
  • 7
  • 23
  • See also [How to access the address of a static const member of a class?](https://stackoverflow.com/questions/18107077/how-to-access-the-address-of-a-static-const-member-of-a-class) and in particular [this answer](https://stackoverflow.com/a/18107129). – dxiv Jul 20 '20 at 00:14
  • From [cppreference](https://en.cppreference.com/w/cpp/language/static): _"The `static` keyword is only used with **the declaration of a `static` member, inside the class definition**, but not with the definition of that static member [...] If a static data member of integral or enum type is declared `const` (and not `volatile`), it can be initialized with an initializer [constant expression], right inside the class definition: [...]"_. Thus, even if you use a constant expression to _initialize_ your const static data member of integral type, it is still _not defined_. – dfrib Jul 20 '20 at 00:17
  • 2
    Is it morally allowed to mark as a dup of a non-language-lawyer-tagged question? Should be like dup of [Trying to understand \[basic.def.odr\]/2 in C++14 (N4140)](https://stackoverflow.com/questions/29997142/trying-to-understand-basic-def-odr-2-in-c14-n4140) or smth similar. – Language Lawyer Jul 20 '20 at 00:18
  • @LanguageLawyer As this is quite a trivial one, maybe we could either A) remove the language-lawyer tag from this question, or B) consider adding the language-lawyer tag to the linked question (solely due to the fact that the answer contains the relevant standard passages that would entirely answer this trivial LL-question)? – dfrib Jul 20 '20 at 00:21
  • @dfri _the answer contains the relevant standard passages_ Uhm. I don't see answers to "Undefined reference to static class member" containing passages from the standard. – Language Lawyer Jul 20 '20 at 00:24
  • @LanguageLawyer The question is surely not asking for it, but [this answer in particular](https://stackoverflow.com/a/18107129/4573247) provides it. – dfrib Jul 20 '20 at 00:25
  • @dfri Ah, I've thought you mean the "Undefined reference to static class member" question. – Language Lawyer Jul 20 '20 at 00:26
  • @LanguageLawyer Btw, as the language lawyer, do you have any insight into [this Q&A and comment](https://stackoverflow.com/questions/62409234/function-template-overload-resolution-dependent-and-non-dependent-parameters/62409289#comment110588229_62415177)? Is the answer in fact straight-forward and without any vague passages in the standard? – dfrib Jul 20 '20 at 00:40
  • @dfri I don't know templates and overload resolution at a language lawyer level, sry – Language Lawyer Jul 20 '20 at 01:07
  • @LanguageLawyer I see. Thanks nonetheless for having a look. – dfrib Jul 20 '20 at 01:42

0 Answers0