【问题标题】:Using SFINAE to test if a pointer type can be static_cast to another pointer type使用 SFINAE 测试指针类型是否可以静态转换为另一种指针类型
【发布时间】:2018-05-22 17:15:58
【问题描述】:

背景

我正在写一个可移动的QScopedPointer;基本上std::unique_pointer 带有一些额外的访问器。我在获得与 C++11 兼容的编译器之前就开始了它,但现在我决心把它做好(即使我正在重新发明轮子)。

问题

让我们调用我的智能指针MyUniquePointer

我需要知道U *类型是否可以通过static_cast转换成T *类型,具体来说:

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()))
{}
...

问题的前半部分

我的第一次尝试是在enable_if 内使用static_cast,但您不能使用std::declval() 的地址来获取static_cast 的指针!

有没有办法使用模板魔术测试指向U 的指针是否可以static_cast 指向指向T 的指针?

尝试的解决方法

基于cppreferencethis answer,我尝试创建一个模板测试来模拟static_cast 何时合法,以及如果向下转换,则安全。到目前为止,这是我汇总的内容:

#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;
}

Wandbox Link

返回:

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

我的问题的后半部分是这个解决方法是否在正确的轨道上,更具体地说,如果|| std::is_convertible&lt;From, To&gt;() 应该是 包括。是否可以让is_convertible 在通过时返回 true 指向类型的指针作为模板参数?上面的代码包括我自己尝试让它工作的笨拙的尝试。

脚注:我知道base_type * 成功转换为derived_type *,但我不是编译器,无法做出这样的假设。

【问题讨论】:

  • "你不能取std::declval()的地址"&amp;std::declval&lt;T&amp;&gt;() 或只是 std::declval&lt;T*&gt;() ?
  • &amp;std::declval&lt;T&gt;()
  • 那行不通,但建议的变体应该可以完成这项工作。
  • 现在看,我不明白std::declval&lt;T*&gt;() 会让你假装构造一个指向假装对象的指针。我想你只会得到一个 T 类型的假指针,它指向任何东西。

标签: c++ c++14 sfinae static-cast


【解决方案1】:

看来你想要:

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
{};

Demo

【讨论】:

  • 哇。 void_t 救援。我明白了,但是您能否添加解释以便我接受它作为答案?
  • void_t是使用 SFINAE 的一种方式。只有当static_cast 正确时,专业化才会形成良好的格式,否则,我们会退回到false_type 的通用情况。
猜你喜欢
  • 1970-01-01
  • 2020-12-14
  • 2012-03-14
  • 2010-10-08
  • 2014-08-15
  • 1970-01-01
相关资源
最近更新 更多