I have come across an unusual syntax in a C++ class at work. (It is valid and godbolt will compile it.)
The assignment operators use a trailing ampersand before specifying the functions that should be deleted. Removing the ampersands seems to result in the same behavior.
What do the ampersands do?
#include <memory>
class CustomClass
{
public:
CustomClass() = default;
virtual ~CustomClass() = default;
CustomClass(const CustomClass&) = delete;
CustomClass(CustomClass&&) = delete;
CustomClass& operator=(const CustomClass&) & = delete;
// What is this ---------------------------^
CustomClass& operator=(CustomClass&&) & = delete;
// What is this ----------------------^
};
int main()
{
CustomClass cc{};
CustomClass cc2{};
cc = cc2; // Error: Use of operator=(const CustomClass&)
cc = std::move(cc2); // Error: Use of operator=(CustomClass&&)
return 0;
}