PrevUpHomeNext

Restrict default assignment operators, c++29


> Start
> Back: Home

Current c++ permits various ways of defaulted assignment operators of class.

Such as these permit implausible declarations:

A & operator=(const A &) && = default;

B & operator=(const B &) const = default;

Where A, B are c++ classes.

Proposed c++29 will forbit such declarations.

Some divergences

Code

Current

Proposed

A & operator=(A &) = default;

OK

OK

template <typename>
class A
{
public:
	static const A & fn();
	A & operator=(decltype(A::fn())) = default;
};

gcc: Invalid
msvc: Invalid
edg: Invalid

OK

A & operator=(const A &&) = default;

OK

Invalid

A & operator=(const A &) const = default;

OK

Invalid

A & operator=(const A &) && = default;

OK

Invalid

A && operator=(const A &) && = default;

Invalid

Invalid

class M
{
public:
	M & operator=(const M &) volatile;
};
class A
{
public:
	volatile M m;
	A & operator=(const A &) = default;
};

Deleted

Deleted

class A
{
public:
	A & operator=(A &);
};
class B
{
public:
	A a;
	B & operator=(const B &) = default;
};

Deleted

Deleted

class A
{
public:
	A & operator=(A &);
};
class B
{
public:
	A a;
	B & operator=(const B &);
};
B & B::operator=(const B &) = default;

Invalid

Invalid

Gcc - The gnu c++ compiler:

https://gcc.gnu.org

///////////////////////////////////////////////////////////////////////////////

Mon Nov 16 01:59:24 AM UTC 2026

c++ Object-Oriented
Template
Concepts
Coroutines
Reflection
Tools B2 Build
Boost Quickbook
Kakoune
Ugrep
Libraries Boost
boost::asio
Asio
boost::beast
World cppHot
+ sitemap
+ @cppHot
https://www.cpphot.tiv.cc


PrevUpHomeNext