Is there a reason (other than because the standard says so) to why the following code is not allowed?
struct Foo
{
~Foo() && {}
~Foo() & {}
};
I know that it is illegal, but I wanna know why.
I was thinking about the good old avoid unnamed instances problem, i.e. when using guard objects, like:
void do_something()
{
std::lock_guard{my_mutex};
// some synchronized operation
}
This is legal code but obviously error prone since the lock guard would be destroyed immediately after its construction, because it's a temporary (unnamed) object.
I was planning on doing something like this
struct Foo
{
~Foo() && = delete;
~Foo() & = default;
};
and get a compiler error if the type is constructed as a temporary.