[RFC] Using std::unique_ptr and std::make_unique in our code
Pedro Alves
pedro@palves.net
Tue Jul 12 11:01:54 GMT 2022
On 2022-07-12 11:45 a.m., Jonathan Wakely wrote:
> On Tue, 12 Jul 2022 at 11:22, Florian Weimer via Gcc <gcc@gcc.gnu.org> wrote:
>>
>> * Pedro Alves:
>>
>>> For example, for the type above, we'd have:
>>>
>>> typedef std::unique_ptr<pending_diagnostic> pending_diagnostic_up;
>>>
>>> and then:
>>>
>>> - pending_diagnostic *d,
>>> + pending_diagnostic_up d,
>>>
>>> I would suggest GCC have a similar guideline, before people start
>>> using foo_ptr, bar_unp, quux_p, whatnot diverging styles.
>>
>> This doesn't seem to provide much benefit over writing
>>
>> uP<pending_diagnostic> d;
>>
>> and with that construct, you don't need to worry about the actual
>> relationship between pending_diagnostic and pending_diagnostic_up.
>>
>> I think the GDB situation is different because many of the types do not
>> have proper destructors, so std::unique_ptr needs a custom deleter.
>
>
> A fairly common idiom is for the type to define the typedef itself:
>
> struct pending_diagnostic {
> using ptr = std::unique_ptr<pending_diagnostic>;
> // ...
> };
>
> Then you use pending_diagnostic::ptr. If you want a custom deleter for
> the type, you add it to the typedef.
>
> Use a more descriptive name like uptr or uniq_ptr instead of "ptr" if
> you prefer.
>
Only works if you can change the type, though. Sometimes you can't,
as it comes from a library.
More information about the Gcc
mailing list