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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:35:43+00:00 2026-06-06T03:35:43+00:00

Is the code below bad practice or undefined behavior? Essentially i am calling a

  • 0

Is the code below bad practice or undefined behavior? Essentially i am calling a const func to modify a member which is not marked as mutable. Link to demo

Credits to Mehrdad for inspiring this question (his question Does this code subvert the C++ type system?) and david for minor demo improvements.

#include <iostream>
using namespace std;

struct BreakConst
{
    int v;
    int *p;
    BreakConst() { v = 0; p = &v; } 
    void break_stuff() const { ++*p; }
};
void f(const BreakConst& bc) {
    bc.break_stuff();
}

Original Version that most answers are based on:

Answered by David Rodríguez, jpalecek, Mehrdad
Yes: This is “Undefined behavior”

int main()
{
    const BreakConst bc;
    cout << bc.v << endl;   // 0
    bc.break_stuff();       // O:)
    cout << bc.v << endl;   // 1

    return 0;
}

New Alternative Question:

Answered by Mehrdad
No: This is not “Undefined behavior”

int main()
{
    BreakConst bc;
    cout << bc.v << endl;   // 0
    f(bc);                  // O:)
    cout << bc.v << endl;   // 1

    return 0;
}

Result:

0
1
  • 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-06T03:35:45+00:00Added an answer on June 6, 2026 at 3:35 am

    In this case, I’d say it’s undefined behaviour. bc is a const object, so are all its subobjects (less mutables)1, therefore bc.v should be, too and modifying a const object, however achieved, is UB2.

    [1] C++03 3.9.3/3:

    Each non-static, non-mutable, non-reference data member of a const-qualified class object is const-
    qualified…

    [2] C++03 7.1.5.1/4:

    Except that any class member declared mutable (7.1.1) can be modified, any attempt to modify a const
    object during its lifetime (3.8) results in undefined behavior.

    EDIT responding to the edit of the question: No, the modified version of the code does not cause undefined behavior. It may be bad practice, but actually may be useful at times. You can eg. use it to implement iterators to your classes via const-iterators (DRY):

    class const_iterator
    {
    public:
      const T& dereference() const; // complicated
    };
    
    class iterator : public const_iterator
    {
    public:
      T& dereference() const { return const_cast<T&>(const_iterator::dereference()); }
    };
    

    Of course that relies on the fact that iterators can only be made from mutable containers, that the const and non-const versions do not differ (no COW and such) etc., but that is fairly common.

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

Sidebar

Related Questions

Which one of the below 2 code pieces is not calling dispose and therefore
Is it a bad practice to do what the code below does? Will bad
Code below, not sure what i'm doing wrong. It is an employee database. The
The code below are to validate a set of selectbox, however, it will not
I have a C++ code mentioned below: #include<iostream> #include<stdexcept> const long MAX = 10240000;
Is it considered bad practice (VB.NET or any language) to code a function with
In the code below; any idea why the ifs becomes bad when std::copy is
while trying to reload table the program giving BAD_EXCESS Signal. In below code. -(void)textFieldDidEndEditing:(UITextField
Code below: [HttpGet] public ActionResult Edit(string id=) { // ... } [HttpPost] public ActionResult
Code below is working well as long as I have class ClassSameAssembly in same

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.