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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T08:47:11+00:00 2026-05-14T08:47:11+00:00

Consider the following code: struct Foo { mutable int m; template<int Foo::* member> void

  • 0

Consider the following code:

struct Foo
{
    mutable int m;

    template<int Foo::* member> 
    void change_member() const {
        this->*member = 12; // Error: you cannot assign to a variable that is const
    }

    void g() const {
    change_member<&Foo::m>();
    }
};

Compiler generates an error message. The thing is that the member m is mutable therefore it is allowed to change m. But the function signature hides mutable declaration.

How to decalre pointer-to-mutable-member to compile this code?
If it is impossible please link to Standard C++.

  • 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-14T08:47:11+00:00Added an answer on May 14, 2026 at 8:47 am

    This code is ill-formed according to C++ Standard 5.5/5:

    The restrictions on cv-qualification,
    and the manner in which the
    cv-qualifiers of the operands are
    combined to produce the cv-qualifiers
    of the result, are the same as the
    rules for E1.E2 given in 5.2.5. [Note:
    it is not possible to use a pointer to member that refers to a mutable
    member to modify a const class
    object.

    For example,

    struct S {
      mutable int i;
    };
    const S cs;
    int S::* pm = &S::i; // pm refers to mutable member S::i
    cs.*pm = 88;         // ill-formed: cs is a const object
    

    ]

    You could use wrapper class to workaround this problem as follows:

    template<typename T> struct mutable_wrapper { mutable T value; };
    
    struct Foo
    {
        mutable_wrapper<int> m;
    
        template<mutable_wrapper<int> Foo::* member> 
        void change_member() const {
            (this->*member).value = 12; // no error
        }
    
        void g() const {
        change_member<&Foo::m>();
        }
    };
    

    But I think you should consider redesign your code.

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

Sidebar

Ask A Question

Stats

  • Questions 445k
  • Answers 445k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer partitioned functions introduced in SQL 2005 don't support this AFAIK… May 15, 2026 at 6:50 pm
  • Editorial Team
    Editorial Team added an answer Rundll32 only supports running DLL exports with the following signature:… May 15, 2026 at 6:50 pm
  • Editorial Team
    Editorial Team added an answer There are a few bugs here working together to give… May 15, 2026 at 6:50 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.