<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On 5 October 2017 at 03:31, Ville Voutilainen <span dir="ltr"><<a href="mailto:ville.voutilainen@gmail.com" target="_blank">ville.voutilainen@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">It occurs to me that it would be useful to have a feature macro for<br>
mandatory copy elision. This came up<br>
in Qt: <a href="https://codereview.qt-project.org/#/c/206467/" rel="noreferrer" target="_blank">https://codereview.qt-project.<wbr>org/#/c/206467/</a><br>
<br>
So, we want to have a RAII handle that should not easily traverse<br>
scopes. Since the RAII handle<br>
wishes to store an arbitrary functor, we need to make it a template.<br>
In order to avoid having to<br>
force users to specify complex template arguments, we want to add a<br>
factory function (we could<br>
use Class Template Argument Deduction but that's another thing with<br>
another macro). For C++17,<br>
the factory can just return a noncopyable nonmovable type. For earlier<br>
versions, we can befriend<br>
the factory and require that users use lifetime-extending references<br>
for the factory's return value.<br>
<br>
With the envisioned macro, the class would look roughly like this,<br>
with the template bits removed for<br>
brevity:<br>
<br>
struct DoesNotCopyNorMove<br>
{<br>
DoesNotCopyNorMove() = default;<br>
#ifdef __cpp_guaranteed_copy_elision<br>
DoesNotCopyNorMove(const DoesNotCopyNorMove&) = delete;<br>
DoesNotCopyNorMove(<wbr>DoesNotCopyNorMove&&) = delete;<br>
#else<br>
private:<br>
DoesNotCopyNorMove(const DoesNotCopyNorMove&);<br>
DoesNotCopyNorMove(<wbr>DoesNotCopyNorMove&&);<br>
friend DoesNotCopyNorMove factory();<br>
#endif<br>
};<br>
<br>
Again, for pre-C++17, using it looks like this:<br>
<br>
auto&& x = factory(); // but NOT auto x = factory();<br>
<br>
With C++17, the same syntax can be used, but by-value use is also possible:<br>
<br>
auto x = factory();<br>
<br>
Thoughts?</blockquote><div><br></div><div>What benefit does the feature-test macro give you over using the non-guaranteed-copy-elision implementation in all cases?</div></div></div></div>