Boost:如果它是一个指针,则取消引用模板参数 [英] Boost: dereference a template argument if it's a pointer
本文介绍了Boost:如果它是一个指针,则取消引用模板参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果模板参数是指针(或智能指针),我可以使用什么来取消引用模板参数,或者如果不是,则可以保留原样。
What can I use to dereference a template argument if it's a pointer (or a smart pointer), or leave it as is if it's not?
template<class T> void subf(const T& item)
{
item.foo();
}
template<class T> void f(const T& item)
{
subf(magic_dereference_function(item));
}
Boost中的任何选项都是可选项。
Anything in Boost is an option.
推荐答案
template <typename T>
T& maybe_deref(T& x) { return x; }
template <typename T>
T& maybe_deref(T* x) { return *x; }
您必须为智能指针单独添加重载。没有办法检测一个类是否是一个智能指针。您可以检测到 operator->
的存在,但这并不意味着它是一个智能指针。
You'll have to add overloads for smart pointers individually. There's no way to detected if a class is a "smart pointer". You could detect the presence of operator->
, but that doesn't mean it's a smart pointer.
这篇关于Boost:如果它是一个指针,则取消引用模板参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文