Specializes in std :: make_shared

I have a type with strict alignment requirement (due to the used AVX operations) that is larger than the default alignment for platforms.

To simplify the use of this class, I would like to specialize std::make_sharedto always use a suitable allocator for this type.

Something like that:

namespace std{
    template<class... Args> inline
    auto make_shared<X, Args...>(Args&&... args){
        return std::allocate_shared(allocator_type<X, 32>, std::forward<Args>(args)...);
    }
}

My question is: is this permitted by the standard? Will it work as expected?

+4
source share
2 answers

From N4140 [namespace.std] / 1 (emphasis added):

++ undefined, std std, . std, , .

, , std.

, @dyp, . , X ( ) make_shared_x ( ).

+9

, , :

namespace xtd{
    template< typename T, std::size_t align = std::alignment_of<T>::value, typename... Args >
    std::shared_ptr<T> make_shared(Args&&... args){
        // Platform specific knowledge.
#if defined(_WIN64) || defined(_WIN32)
#if defined(_WIN64)
        const std::size_t default_alignment = 16;
#else
        const std::size_t default_alignment = 8;
#endif
#else
#error "Only windows for now"
#endif

        if (align > default_alignment) {
            typedef aligned_allocator<T, align> alloc_type;
            return std::allocate_shared<T, alloc_type>(alloc_type(), std::forward<Args>(args)...);
        }
        else {
            return std::make_shared<T>(std::forward<Args>(args)...);
        }
    }
}

std::make_shared xtd::make_shared:)

, ...

+5

Source: https://habr.com/ru/post/1598355/


All Articles