Reputation: 109
I have some doubt, I writing some code(for receive the naked pointer), in C++ something like this:
template< typename T >
auto unwrap_ptr(T smart_ptr){
//if it some class which store pointer
//receive this pointer with method get()
return smart_ptr.get();
}
template< typename T >
auto unwrap_ptr(T *nake_ptr){
//if it naked(simple) pointer return this pointer
return nake_ptr;
}
If it function take naked pointer it return this pointer, in otherwise it return ptr from get() method. And after check it in my mind appear doubt. In my machine and compiler it will work, but what about other compiler? What say about this situation standard? It is UB? Will be in some case call first function if I put in function naked pointer? And second little question(sorry that they are in one place) what will be here
template< typename T >
bool same_type(T x, T y) {
//if x and y same type(class) return true
return true;
}
template< typename X, typename Y >
bool same_type(X x, Y y) {
//if x and y different type(class) return false
return false;
}
It will be work how I think or in some cases it will be dangerous code?
Upvotes: 2
Views: 61
Reputation: 385144
If it function take naked pointer it return this pointer, in oterwise it return ptr from get() method. And after chek it in my mind appear doubt. In my machine and compiler it will work, but what about other compiler? What say about this situation standart? It is UB? Will be in some case call first function if I put in function naked pointer?
This approach is reasonable, well-defined and portable. No UB!
However, it is limited in that you won't be able to pass a unique_ptr
in, because a unique_ptr
cannot be copied. Consider taking const T&
into the first function, then add another one that takes const T&&
but is deleted (to protect against temporaries, for which this function is very dangerous due to the probable dangling pointer!).
template< typename T >
auto unwrap_ptr(const T& smart_ptr){
//if it some class wich store pointer
//receive this pointer with method get()
return smart_ptr.get();
}
template< typename T >
auto unwrap_ptr(const T&& smart_ptr) = delete;
template< typename T >
auto unwrap_ptr(T *nake_ptr){
//if it naked(simple) pointer return this pointer
return nake_ptr;
}
#include <memory>
int main()
{
auto sptr = std::make_unique<int>(42);
int x = 43;
unwrap_ptr(sptr); // ok
//unwrap_ptr(std::make_unique<int>(42)); // prohibited
unwrap_ptr(&x); // ok
}
C++20 will have std::to_address
which does the same thing, though at first glance it doesn't seem to have the above protection against temporaries which is a shame. It's probably a case of "let the programmer deal with this problem, if they need to"; ultimately, you are going to have to be careful about the lifetime of the returned raw pointer anyway.
And second little question(sorry that they are in one place) what will be here
It will be work how I think or in some cases it will be dangerous code?
Yes, that's fine too.
However we would typically use std::is_same_v
for this. To bring deduction back into it you could wrap it:
#include <type_traits>
template <typename X, typename Y>
bool same_type(const X&, const Y&)
{
return std::is_same_v<X, Y>;
}
Upvotes: 3