cuda::experimental::stf::to_shared

Defined in include/cuda/experimental/__stf/utility/core.cuh

template<typename T>
auto cuda::experimental::stf::to_shared(T &&obj)

Creates a std::shared_ptr managing a copy of the given object.

This function takes an object of any type and returns a std::shared_ptr that manages a copy of that object. If the object is an lvalue reference, it will be copied into the shared_ptr. If the object is an rvalue reference, it will be moved into the shared_ptr.

The type managed by the shared_ptr has all references and const/volatile qualifiers removed from the original type.

int value = 42;
auto sp1 = to_shared(value);            // New shared_ptr<int>
assert(*sp1 == 42);                     // sp1 points to an int valued at 42

Note

This function simplifies the creation of std::shared_ptrs by handling the type deduction and appropriate forwarding of the object. It’s particularly useful when you want to create a shared_ptr from temporary objects or when the object’s type includes references or cv-qualifiers.

Template Parameters

T – The type of the object, deduced automatically. May be an lvalue or rvalue reference.

Parameters

obj – The object to copy into the instance managed by the shared_ptr.

Returns

A std::shared_ptr managing a new copy of the object.