Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does =default on operator= compile when there is a const member?

Tags:

c++

c++11

class Foo {
public:
  Foo& operator=(const Foo&) = default;
private:
  const int i = 0;
};

Why is =default allowed there? It compiles without errors. I would think that =default should fail since it's not possibly to assign into the const variable?

What actually is happening?

like image 314
user2015453 Avatar asked Feb 01 '13 14:02

user2015453


1 Answers

When the function cannot be generated (as is the case), = default will generate it as = deleted instead. If you try to use that assignment operator your compiler should produce an error.

like image 58
R. Martinho Fernandes Avatar answered Sep 22 '22 11:09

R. Martinho Fernandes