C++ Logo

SG10

Advanced search

Subject: Re: [SG10] A feature macro for mandatory copy elision
From: Nelson, Clark (clark.nelson_at_[hidden])
Date: 2017-10-05 14:02:05


Unless anyone expresses an objection to this idea, I'm going to incorporate it. I'll probably post an updated draft tomorrow.

After that I will start on updating the section references in the C++17 table.

Clark

> -----Original Message-----
> From: features-bounces_at_[hidden] [mailto:features-bounces_at_open-
> std.org] On Behalf Of Ville Voutilainen
> Sent: Thursday, October 05, 2017 03:31
> To: Features_at_[hidden]
> Subject: [SG10] A feature macro for mandatory copy elision
>
> It occurs to me that it would be useful to have a feature macro for
> mandatory copy elision. This came up
> in Qt: https://codereview.qt-project.org/#/c/206467/
>
> So, we want to have a RAII handle that should not easily traverse
> scopes. Since the RAII handle
> wishes to store an arbitrary functor, we need to make it a template.
> In order to avoid having to
> force users to specify complex template arguments, we want to add a
> factory function (we could
> use Class Template Argument Deduction but that's another thing with
> another macro). For C++17,
> the factory can just return a noncopyable nonmovable type. For
> earlier
> versions, we can befriend
> the factory and require that users use lifetime-extending references
> for the factory's return value.
>
> With the envisioned macro, the class would look roughly like this,
> with the template bits removed for
> brevity:
>
> struct DoesNotCopyNorMove
> {
> DoesNotCopyNorMove() = default;
> #ifdef __cpp_guaranteed_copy_elision
> DoesNotCopyNorMove(const DoesNotCopyNorMove&) = delete;
> DoesNotCopyNorMove(DoesNotCopyNorMove&&) = delete;
> #else
> private:
> DoesNotCopyNorMove(const DoesNotCopyNorMove&);
> DoesNotCopyNorMove(DoesNotCopyNorMove&&);
> friend DoesNotCopyNorMove factory();
> #endif
> };
>
> Again, for pre-C++17, using it looks like this:
>
> auto&& x = factory(); // but NOT auto x = factory();
>
> With C++17, the same syntax can be used, but by-value use is also
> possible:
>
> auto x = factory();
>
> Thoughts?
> _______________________________________________
> Features mailing list
> Features_at_[hidden]
> http://www.open-std.org/mailman/listinfo/features


SG10 list run by herb.sutter at gmail.com