Re: [RFA 02/22] Use RAII to save and restore scalars

Pedro Alves
2016-10-01T10:33:31Z

On 10/01/2016 05:23 AM, Tom Tromey wrote:
> Pedro> Guess we could have the copy constructor actually move instead of
> Pedro> copy, setting the copied-from's pointer member to NULL, and then
> Pedro> have the destructor do nothing if the pointer is NULL.
>
> Tom> Yeah. I took this approach. I'm not super thrilled with it, but at the
> Tom> same time it's not *too* bad.
>
> Scratch that, I spoke too soon. I couldn't get this to work.
>
> I don't understand why the 2-argument form of make_scoped_restore
> requires a copy constructor while the 1-argument form does not.
>
> However, once you need a copy constructor, it doesn't seem possible
> without a move constructor. The issue is that a copy constructor take a
> const reference, so you can't modify the fields of the original.

You can -- make the fields mutable. That's what ScopeGuard does too.

>
> Removing the 2-argument form seems to work. Though like I said, I don't
> know why.

I can't tell off hand. Maybe seeing the code would suggest something.

>
>
> Also, as an aside, I found I was using nullptr in my patches, but this
> isn't C++03. I think -std=c++03 is going to be needed or else it will
> be too easy to slip in C++11-isms. Now, I do think C++11 is really much
> better, but my understanding is that C++03 is what gdb decided on.

I'm very much against forcing -std=c++03.

My plan is to allow compiling with C++11 too, and use
some of its features, for extra safety and extra efficiency,
as long as we have C++03 fallbacks in place. If we force -std=c++03,
that becomes impossible.

I recently elaborated on this here:
https://gcc.gnu.org/ml/gcc-patches/2016-08/msg01624.html

The gist is that I think that the features supported by compilers that
people are using in practice to build gdb with is what matters
in the end, not really C++03 vs C++11.

The only reason we're not requiring C++11 is that some people
still wanted to build gdb with older compilers that don't support it.
That was last revisited a year ago when we discussed whether to
go C++ or not in the first place. It may be that the baseline might have
advanced meanwhile.

Thanks,
Pedro Alves