[英]Computing the type of a function pointer
考虑以下:
template<typename T>
struct S
{
typedef M< &T::foo > MT;
}
这将适用于:
S<Widget> SW;
其中Widget::foo()
是一些函数
我将如何修改struct S
的定义以允许以下内容:
S<Widget*> SWP;
您需要以下类型转换。
T
,返回T
T *
,返回T
碰巧的是,标准库已经在std::remove_pointer
为我们实现了这std::remove_pointer
(尽管自己做起来并不难)。
有了这个,你可以写
using object_type = std::remove_pointer_t<T>;
using return_type = /* whatever foo returns */;
using MT = M<object_type, return_type, &object_type::foo>;
关于您还想使用智能指针的评论,我们必须重新定义类型转换。
smart_ptr<T>
,返回smart_ptr<T>::element_type
,应为T
T *
,返回T
T
,返回T
本身 为此,我们必须编写我们自己的元函数。 至少,我不知道标准库中有什么对这里有帮助的。
我们首先定义主要template
(“否则”情况)。
template <typename T, typename = void>
struct unwrap_obect_type { using type = T; };
默认为void
的第二个(匿名)类型参数将在以后使用。
对于(原始)指针,我们提供以下部分专业化。
template <typename T>
struct unwrap_obect_type<T *, void> { using type = T; };
如果我们在这里停止,基本上就可以得到std::remove_pointer
。 但是,我们将为智能指针添加额外的部分专业化。 当然,我们首先必须定义什么是“智能指针”。 出于本示例的目的,我们将使用嵌套的typedef
命名为element_type
每个类型作为智能指针。 视需要调整此定义。
template <typename T>
struct unwrap_obect_type
<
T,
std::conditional_t<false, typename T::element_type, void>
>
{
using type = typename T::element_type;
};
第二个类型参数std::conditional_t<false, typename T::element_type, void>
是在C ++ 14中模拟std::void_t
的复杂方法。 我们的想法是,我们具有以下部分类型函数。
T
的嵌套typedef
名为element_type
,返回void
因此,如果我们正在处理智能指针,则将获得比主template
更好的匹配,否则,SFINAE将从进一步考虑中删除此部分专业化知识。
这是一个工作示例。 TC建议使用std::mem_fn
调用成员函数。 这使代码比我最初的示例干净得多。
#include <cstddef>
#include <functional>
#include <iostream>
#include <memory>
#include <string>
#include <utility>
template <typename ObjT, typename RetT, RetT (ObjT::*Pmf)() const noexcept>
struct M
{
template <typename ThingT>
static RetT
call(ThingT&& thing) noexcept
{
auto wrapper = std::mem_fn(Pmf);
return wrapper(std::forward<ThingT>(thing));
}
};
template <typename T, typename = void>
struct unwrap_obect_type { using type = T; };
template <typename T>
struct unwrap_obect_type<T *, void> { using type = T; };
template <typename T>
struct unwrap_obect_type<T, std::conditional_t<false, typename T::element_type, void>> { using type = typename T::element_type; };
template <typename T>
struct S
{
template <typename ThingT>
void
operator()(ThingT&& thing) const noexcept
{
using object_type = typename unwrap_obect_type<T>::type;
using id_caller_type = M<object_type, int, &object_type::id>;
using name_caller_type = M<object_type, const std::string&, &object_type::name>;
using name_length_caller_type = M<object_type, std::size_t, &object_type::name_length>;
std::cout << "id: " << id_caller_type::call(thing) << "\n";
std::cout << "name: " << name_caller_type::call(thing) << "\n";
std::cout << "name_length: " << name_length_caller_type::call(thing) << "\n";
}
};
class employee final
{
private:
int id_ {};
std::string name_ {};
public:
employee(int id, std::string name) : id_ {id}, name_ {std::move(name)}
{
}
int id() const noexcept { return this->id_; }
const std::string& name() const noexcept { return this->name_; }
std::size_t name_length() const noexcept { return this->name_.length(); }
};
int
main()
{
const auto bob = std::make_shared<employee>(100, "Smart Bob");
const auto s_object = S<employee> {};
const auto s_pointer = S<employee *> {};
const auto s_smart_pointer = S<std::shared_ptr<employee>> {};
s_object(*bob);
std::cout << "\n";
s_pointer(bob.get());
std::cout << "\n";
s_smart_pointer(bob);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.