On May 15, 12:15*pm, Phil Endecott <android...@chezphil.org> wrote:
> Dear Experts,
>
> Say I have this:
>
> struct scoped_foo {
> * scoped_foo(int n) { enable_foo(n); }
> * ~scoped_foo() { disable_foo(); }
>
> };
>
> and I use it like this:
>
> {
> * scoped_foo(3);
> * blah(); *// foo is enabled here}
>
> // foo is disabled here
>
> ...except that doesn't work, because I should have written
>
> {
> * scoped_foo ANY_NAME_HERE(3);
> * ....
>
> I've now made this mistake a few times, and it's annoying.
>
> So, my question: is there anything that I can do in my declaration of
> scoped_foo so that trying to use it in this wrong way, i.e. to create
> a temporary rather than an object with scope up to the next }, will
> give an error, or at least a warning (with g++)?
This would be a great use for lvalue-ref qualifiers on destructors:
~scoped_foo()& { disable_foo(); }
Unfortunately this use of reference qualifiers is disallowed.
Perhaps you could propose it.
Disclaimer: This is off the cuff. I haven't fully investigated such
proposed use.
Howard