Feb 10, 2022

[C++] ref data member is not const inside a const member function due to C++'s type system

Data member with reference type is not const inside a const member function due to C++'s type system.

i.e

c.v is decorated from right to left thus

int& const data_member;

is not valid.

struct Fun {
  int &a;
  void run() const { a += 1; }
};

int main() {

  int a = 42;
  [&a] { a += 1; }();

  Fun f{a};
  f.run();
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.