Reputation: 4367
I'm writing a take on a moveable QScopedPointer
; basically std::unique_pointer
with some extra accessors. I started it before I had access to a C++11-compatible compiler, but now I'm determined to get it right (even though I'm reinventing the wheel).
Let's call my smart pointer MyUniquePointer
.
I need to know if type U *
can be converted into type T *
via a static_cast
, specifically:
template<class T, class Cleanup>
class MyUniquePointer
{
...
template<class U, class CleanupU, class = std::enable_if_t<detail::is_safely_castable<U, T>()>
MyUniquePointer(MyUniquePointer<U, CleanupU> && other) noexcept
: d(static_cast<T *>(other.release()))
{}
...
My first attempt was to use static_cast
inside of an enable_if
, but you can't take the address of std::declval()
to get a pointer for static_cast
!
Is there a way to use test if a pointer to U
can be static_cast
to a pointer to T
using template magic?
Based on cppreference and this answer, I tried to create a template test to emulate when a static_cast is legal and, if downcasting, safe. Here's what I've put together so far:
#include <iostream>
#include <type_traits>
template <class From, class To>
struct is_safely_castable //should probably be is_safely_castable_pointer or something
: std::integral_constant<bool,
std::is_pointer<From>() && std::is_pointer<To>()
&& ((std::is_base_of<To, From>()/* && std::has_virtual_destructor<From>()*/)
|| std::is_convertible<From, To>()
|| std::is_same<To,void *>()
|| std::is_same<From, void *>())>
{
};
struct base_type
{
base_type() = default;
base_type(base_type &&) = default;
base_type(const base_type &) = default;
virtual ~base_type() { }
base_type &operator=(const base_type &) = default;
base_type &operator=(base_type &&) = default;
};
struct derived_type : public base_type
{
};
struct unrelated_type
{
};
struct convertible_type
{
convertible_type(const base_type *) {}
convertible_type(base_type *) {}
convertible_type() = default;
operator base_type *() { return nullptr; }
};
int main(int argc, char *argv[])
{
(void)(argc);
(void)(argv);
base_type *b = new base_type;
derived_type *d = new derived_type;
unrelated_type *u = new unrelated_type;
uint32_t *i32 = new uint32_t{1};
uint64_t *i64 = new uint64_t{2};
void *v = static_cast<derived_type *>(d);
std::cout << std::boolalpha
<< "Base to base: " << (bool)static_cast<base_type *>(b) << '\n'
<< "Base to derived: " << (bool)static_cast<derived_type *>(b) << '\n'
<< "Derived to base: " << (bool)static_cast<base_type *>(d) << '\n'
<< "Unrelated to base: false\n" //<< static_cast<base_type *>(u) << '\n'
<< "uint32 to uint64: false\n" //<< static_cast<uint64_t *>(i32) << '\n'
<< "uint64 to uint32: false\n" //<< static_cast<uint32_t *>(i64) << '\n'
<< "Base to void: " << (bool)static_cast<void *>(b) << '\n'
<< "Void to derived: " << (bool)static_cast<derived_type *>(v) << '\n'
<< "Convertible to base: false\n" //<< static_cast<base_type *>(c) << '\n'
<< "Base to convertible: false\n";//<< static_cast<convertible_type *>(b) << '\n';
std::cout << "-----------\n"
<< "Base to base: " << is_safely_castable<base_type *, base_type *>() << '\n'
<< "Base to derived: " << is_safely_castable<base_type *, derived_type *>() << '\n'
<< "Derived to base: " << is_safely_castable<derived_type *, base_type *>() << '\n'
<< "Unrelated to base: " << is_safely_castable<unrelated_type *, base_type *>() << '\n'
<< "uint32 to uint64: " << is_safely_castable<uint32_t *, uint64_t *>() << '\n'
<< "uint64 to uint32: " << is_safely_castable<uint64_t *, uint32_t *>() << '\n'
<< "Base to void: " << is_safely_castable<base_type *, void *>() << '\n'
<< "Void to derived: " << is_safely_castable<void *, derived_type *>() << '\n'
<< "Convertible to base: " << is_safely_castable<convertible_type *, base_type *>() << '\n'
<< "Base to convertible: " << is_safely_castable<base_type *, convertible_type *>() << '\n';
delete b;
delete d;
delete u;
delete i32;
delete i64;
return 0;
}
which returns:
Base to base: true
Base to derived: true
Derived to base: true
Unrelated to base: false
uint32 to uint64: false
uint64 to uint32: false
Base to void: true
Void to derived: true
Convertible to base: false
Base to convertible: false
-----------
Base to base: true
Base to derived: false
Derived to base: true
Unrelated to base: false
uint32 to uint64: false
uint64 to uint32: false
Base to void: true
Void to derived: true
Convertible to base: false
Base to convertible: false
The second half to my questions is if this workaround is on the right track, and, more specifically, if || std::is_convertible<From, To>()
should be
included. Is it possible to have is_convertible
return true when passed
pointers to types as template parameters? The code above includes my own ham-handed attempt to get it to work.
Footnote: I'm aware that base_type *
casts to derived_type *
successfully, but I'm not the compiler and cannot make that assumption.
Upvotes: 2
Views: 964
Reputation: 217810
It seems you want:
template <typename T, typename U, typename = void>
struct is_safely_castable : std::false_type {};
template <typename T, typename U>
struct is_safely_castable<T, U,
std::void_t<decltype(static_cast<U>(std::declval<T>()))>>
: std::true_type
{};
Upvotes: 6