Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6917705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:45:51+00:00 2026-05-27T09:45:51+00:00

I am compiling the following simple program with g++-4.6.1 –std=c++0x : #include <algorithm> struct

  • 0

I am compiling the following simple program with g++-4.6.1 --std=c++0x:

#include <algorithm>

struct S
{
    static constexpr int X = 10;
};

int main()
{
    return std::min(S::X, 0);
};

I get the following linker error:

/tmp/ccBj7UBt.o: In function `main':
scratch.cpp:(.text+0x17): undefined reference to `S::X'
collect2: ld returned 1 exit status

I realize that inline-defined static members do not have symbols defined, but I was under the (probably flawed) impression that using constexpr told the compiler to always treat the symbol as an expression; so, the compiler would know that it is not legal to pass a reference to the symbol S::X (for the same reason you can’t take a reference to the literal 10).

However if S is declared as namespace, i.e. “namespace S” instead of “struct S”, everything links fine.

Is this a g++ bug or do I still have to use a trick to workaround this annoyance?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T09:45:51+00:00Added an answer on May 27, 2026 at 9:45 am

    I don’t think this is a bug. If you change the constexpr to const, it still fails, with the exact same error.

    You’ve declared S::X, but not defined it anywhere, so there’s no storage for it. If you do anything with it that needs to know the address of it then you’ll need to define it somewhere also.

    Examples:

    int main() {
          int i = S::X; // fine
          foo<S::X>(); // fine
          const int *p = &S::X; // needs definition
          return std::min(S::X, 0); // needs it also
    }
    

    The reason for this is that constexpr can be evaluated at compile time, but it’s not required to be evaluated as such, and can equally happen at runtime. It doesn’t instruct “the compiler to always treat the symbol as an expression”, it hints that it would be sensible and permissible to do so if the compiler felt like it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's a simple example: #include <stdlib.h> int main(void) { _set_error_mode(_OUT_TO_STDERR); return EXIT_SUCCESS; } When
Consider a simple program in C++: #include <iostream> class link {}; int main() {
Let's consider this simple test program: #include <stdio.h> #include <string.h> int main(int argc, char
I have a very simple C++ application. #include <stdio.h> #include <iostream> int main(int argc,
I am getting the following error while compiling a sample program in Qt 4.5
When compiling the following code: void DoSomething(int Numbers[]) { int SomeArray[] = Numbers; }
I'm having problems compiling the following program. I'm using gcc -framework Foundation inherit8.1m and
Will the following program cause any problem during compiling and execution process? class A{
I get the following error when compiling a simple C# console app from MSDN
When compiling the following simpleType with the XJC compile (from the JAXB package)... <xs:simpleType

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.