Say I have two functions:
const char* get_string(int id);
bool free_string(const char* str);
I want to write a std::unique_ptr
wrapper for them.
From this answer I created the following:
template <auto fn>
struct deleter_from_fn {
template <typename T>
constexpr void operator()(T* arg) const {
fn(arg);
}
};
using my_string_unique_ptr =
std::unique_ptr<const char, deleter_from_fn<free_string>>;
But then I thought, it's not a pointer to a const char
, it's a pointer to an array of characters. So I replaced it with the following:
using my_string_unique_ptr =
std::unique_ptr<const char[], deleter_from_fn<free_string>>;
Both seem to work just fine. So my questions are:
- Which variant is preferred?
- Is there any actual difference between the two variants?