Skip to content

Add [[lifetimebound]] attribute to allocator in make_strong_ptr #44

@kammce

Description

@kammce

This provides a hint to the compiler that the lifetime of the memory resource is bound to the lifetime of the returned strong_ptr. That should help identify simple cases where the lifetime of the memory resources is less than that of the strong_ptr its bound to. This can provide a better diagnostic to users at compile time rather than telling the user at runtime that their application terminated due to a resource leak.

We can adopt a macro for selecting the correct attribute for the system:

#ifndef __has_cpp_attribute
#define lifetime_bound
#elif __has_cpp_attribute(clang::lifetimebound)
#define lifetime_bound [[clang::lifetimebound]]
#elif __has_cpp_attribute(msvc::lifetimebound)
#define lifetime_bound [[msvc::lifetimebound]]
#elif __has_cpp_attribute(lifetimebound)
#define lifetime_bound [[lifetimebound]]
#else
#define lifetime_bound
#endif

Found from: https://github.com/PacktPublishing/Software-Architecture-with-Cpp-2E/blob/0c4137c64bb77af0c9468a16cb43f61bbd1a3430/Chapter12/lifetime/main.cpp#L7

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions