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

  • Home
  • SEARCH
  • 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 9137241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:04:40+00:00 2026-06-17T09:04:40+00:00

Can someone tell me when are literal classes needed in C++ ? I am

  • 0

Can someone tell me when are literal classes needed in C++?
I am getting a little confused from constexpr constructors, constexpr members, and I can’t see what the point is. I’d like to see some practical use of it.

Also I’d want to know if a set member function needs to be constexpr, i.e.:

constexpr void set_num(int a) { num = a; }
  • 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-17T09:04:41+00:00Added an answer on June 17, 2026 at 9:04 am

    In C++03 this object has dynamic initialization

    struct Data {
      int i;
      int j;
    };
    Data init_data();  // calculate something
    const Data data = init_data();
    

    i.e. when the program starts, before main runs, the function will be called and the object gets initialized.

    In C++11 the object can have constant initialization, a form of static initialization, meaning that its value is set at compile-time and it’s initialized before the program begins. This is useful to avoid the static initialization order fiasco among other things. To ensure the type gets constant initialization it must be initialized by a constant expression, so must have a constexpr constructor and any functions called in the full expression must be constexpr functions.

    The type Data is trivial so its implicitly-declared constructors are constexpr constructors, so to make the global data undergo constant initialization we just need to make init_data() be a constexpr function:

    struct Data {
      int i;
      int j;
    };
    constexpr Data init_data();  // calculate something
    constexpr Data data = init_data();
    

    The advantage of a literal type is that such types can be used in other constant expressions i.e. in contexts that require compile-time constants. So now that we have our data object as a compile-time constant, we can use it in other constant expressions e.g. to initialize other compile-time constants:

    const int i = ::data.i;
    

    And we can use the Data type for a static data member with an in-class initializer:

    struct MoreData {
      static constexpr Data zerozero = Data{};  // OK, Data is a literal type
    };
    

    If Data wasn’t a literal type we would have to write:

    struct MoreData {
      static const Data zerozero;
    };
    
    // in moredata.cc
    const Data MoreData::zerozero = Data{};
    

    And then code which only sees the header doesn’t know the value of MoreData::zerozero and can’t use it in compile-time optimisations.

    So the advantage of the “literal type” rules is that they allow you to define new class types that can be used in constant expressions. In C++03 only very few types, such as integers, could be used in constant expressions, e.g. integer literals such as 1 or 0x23 or compile-time constants of integer type. In C++11 you can write you own types which can have moderately complicated logic in their constructors (anything that can be expressed in a constexpr function) but can still be used as a compile-time constant.

    Also I’d want to know if a set member function needs to be constexpr, i.e.

    A constexpr member function is a special case of a const member function, so it can’t modify (non-mutable) members of the type. A setter function, which modifies the object, can’t be const.

    To be a literal type a class must follow some rules including having at least one constexpr constructor. That doesn’t mean all objects of that type must be constexpr constants, it just means that objects of that type can be constexpr constants if they are declared as such and are initialized using one of the class’ constexpr constructors. To use the Data example again, most objects in your program would not be constants:

    Data d = { 0, 1 };
    d.i = d.i + 5;
    

    So if you added a setter, a function which modifies the object, then it would only make sense to use it on non-const objects of that type, and like any other functions which modifies the type it should not be

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

Sidebar

Related Questions

Can someone tell me why I'm getting successCallback is not a function in my
Can someone tell me what's wrong in the below code, I am getting syntax
Can someone tell me the regex to see if something is surrounded in double
can someone tell me how to convert code templates from xcode3 to xcode4? I
Can someone tell me how to modify this regex to allow periods in a
can someone tell me what seems to be the issue with this <?php $increment
can someone tell me the order in which a thread starts to execute?. I
Can someone tell me how can I select the Row under the one on
Can someone tell me why this processes all the files and then does it
Can someone tell me why when I cast a string of say 00332 I

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.