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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:08:48+00:00 2026-06-17T22:08:48+00:00

This is probably a bit of an unusual question, in that it asks for

  • 0

This is probably a bit of an unusual question, in that it asks for a fuller explanation of a short answer given to another question and of some aspects of the C++11 Standard related to it.

For ease of reference, I shall sum up the referenced question here. The OP defines a class:

struct Account 
{
    static constexpr int period = 30;
    void foo(const int &) { }
    void bar() { foo(period); } //no error?
};

and is wondering why he gets no error about his usage of an in-class initialized static data member (a book mentioned this to be illegal). Johannes Schaub’s answer states, that:

  1. This violates the One Definition Rule;
  2. No diagnostics is required.

As much as I rely the source and validity of this answer, I honestly dislike it because I personally find it too cryptic, so I tried to work out a more meaningful answer myself, with only partial success. Relevant seems to be § 9.4.2/4:

“There shall be exactly one definition of a static data member that is odr-used (3.2) in a program; no diagnostic is required“ [Emphases are mine]

Which gets me a bit closer to the point. And this is how § 3.2/2 defines an odr-used variable:

“A variable whose name appears as a potentially-evaluated expression is odr-used unless it is an object that satisfies the requirements for appearing in a constant expression (5.19) and the lvalue-to-rvalue conversion (4.1) is immediately applied” [Emphases are mine]

In the OP’s question, variable period clearly satisfies the requirements for appearing in a constant expression, being a constexpr variable. So the reason must be certainly found in the second condition: “and the lvalue-to-rvalue conversion (4.1) is immediately applied“.

This is where I have troubles interpreting the Standard. What does this second condition actually mean? What are the situations it covers? Does it mean that a static constexpr variable is not odr-used (and therefore can be in-class initialized) if it is returned from a function?

More generally: What are you allowed to do with a static constexpr variable so that you can in-class initialize it?

  • 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-17T22:08:49+00:00Added an answer on June 17, 2026 at 10:08 pm

    Does it mean that a static constexpr variable is not odr-used (and
    therefore can be in-class initialized) if it is returned from a
    function?

    Yes.

    Essentially, as long as you treat it as a value, rather than an object, then it is not odr-used. Consider that if you pasted in the value, the code would function identically- this is when it is treated as an rvalue. But there are some scenarios where it would not.

    There are only a few scenarios where lvalue-to-rvalue conversion is not performed on primitives, and that’s reference binding, &obj, and probably a couple others, but it’s very few. Remember that, if the compiler gives you a const int& referring to period, then you must be able to take it’s address, and furthermore, this address must be the same for each TU. That means, in C++’s horrendous TU system, that there must be one explicit definition.

    If it is not odr-used, the compiler can make a copy in each TU, or substitute the value, or whatever it wants, and you can’t observe the difference.

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

Sidebar

Related Questions

I appreciate that this task is probably a bit ambitious given my level (or
I'm a bit new to Rails/RSpec/Capybara, so this is probably a newbie question, but
This is a bit of a random question that is more out of curiosity
This is probably pushing the boundaries of Linq-to-Sql a bit but given how versatile
This is probably a really straightforward answer - but a bit of advice would
This is probably a very basic R question...and feel a bit bad about asking...but
This is probably a bit low question, but is there any elegant way, how
This is probably a bit of a noob question but seeing as I am
This is probably a bit subjective, but I'm in fact looking for answers that
This is probably the MOST basic question and for some reason but I'm a

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.