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 8252519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:35:03+00:00 2026-06-08T00:35:03+00:00

Requirements I want a constexpr value (i.e. a compile-time constant) computed from a constexpr

  • 0

Requirements

I want a constexpr value (i.e. a compile-time constant) computed from a constexpr function. And I want both of these scoped to the namespace of a class, i.e. a static method and a static member of the class.

First attempt

I first wrote this the (to me) obvious way:

class C1 {
  constexpr static int foo(int x) { return x + 1; }
  constexpr static int bar = foo(sizeof(int));
};

g++-4.5.3 -std=gnu++0x says to that:

error: ‘static int C1::foo(int)’ cannot appear in a constant-expression
error: a function call cannot appear in a constant-expression

g++-4.6.3 -std=gnu++0x complains:

error: field initializer is not constant

Second attempt

OK, I thought, perhaps I have to move things out of the class body. So I tried the following:

class C2 {
  constexpr static int foo(int x) { return x + 1; }
  constexpr static int bar;
};
constexpr int C2::bar = C2::foo(sizeof(int));

g++-4.5.3 will compile that without complaints. Unfortunately, my other code uses some range-based for loops, so I have to have at least 4.6. Now that I look closer at the support list, it appears that constexpr would require 4.6 as well. And with g++-4.6.3 I get

3:24: error: constexpr static data member ‘bar’ must have an initializer
5:19: error: redeclaration ‘C2::bar’ differs in ‘constexpr’
3:24: error: from previous declaration ‘C2::bar’
5:19: error: ‘C2::bar’ declared ‘constexpr’ outside its class
5:19: error: declaration of ‘const int C2::bar’ outside of class is not definition [-fpermissive]

This sounds really strange to me. How do things “differ in constexpr” here? I don’t feel like adding -fpermissive as I prefer my other code to be rigurously checked. Moving the foo implementation outside the class body had no visible effect.

Expected answers

Can someone explain what is going on here? How can I achieve what I’m attempting to do? I’m mainly interested in answers of the following kinds:

  • A way to make this work in gcc-4.6
  • An observation that later gcc versions can deal with one of the versions correctly
  • A pointer to the spec according to which at least one of my constructs should work, so that I can bug the gcc developers about actually getting it to work
  • Information that what I want is impossible according to the specs, preferrably with some insigt as to the rationale behind this restriction

Other useful answers are welcome as well, but perhaps won’t be accepted as easily.

  • 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-06-08T00:35:04+00:00Added an answer on June 8, 2026 at 12:35 am

    The Standard requires (section 9.4.2):

    A static data member of literal type can be declared in the class definition with the constexpr specifier; if so, its declaration shall specify a brace-or-equal-initializer in which every initializer-clause that is an assignment-expression is a constant expression.

    In your “second attempt” and the code in Ilya’s answer, the declaration doesn’t have a brace-or-equal-initializer.

    Your first code is correct. It’s unfortunate that gcc 4.6 isn’t accepting it, and I don’t know anywhere to conveniently try 4.7.x (e.g. ideone.com is still stuck on gcc 4.5).

    This isn’t possible, because unfortunately the Standard precludes initializing a static constexpr data member in any context where the class is complete. The special rule for brace-or-equal-initializers in 9.2p2 only applies to non-static data members, but this one is static.

    The most likely reason for this is that constexpr variables have to be available as compile-time constant expressions from inside the bodies of member functions, so the variable initializers are completely defined before the function bodies — which means the function is still incomplete (undefined) in the context of the initializer, and then this rule kicks in, making the expression not be a constant expression:

    an invocation of an undefined constexpr function or an undefined constexpr constructor outside the definition of a constexpr function or a constexpr constructor;

    Consider:

    class C1
    {
      constexpr static int foo(int x) { return x + bar; }
      constexpr static int bar = foo(sizeof(int));
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to validate a string value from textbox. Validation requirements are: Exactly 2
I have 2 requirements: 1) I want to create a new sheet from existing
I want to publish an application which has these requirements: .NET Framework SQL Server
I want to truncate a float value in Java. Following are my requirements: if
I want to put all the requirements of a repoze Zope2 install in a
We want to create a relative simple document storage but there are some requirements.
Requirement is like: I want to take input values from user in frame but
I want to typeset Mathematical problem-sets in Latex. My requirements are as follows: When
Would QuickGraph be able to help me out with my requirements below? (a) want
Requirements I want to publish a Java GUI application on Windows 7. This application

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.