在模板函数中,我试图创建一个std::vector
,其value_type
依赖于函数模板参数的成员函数。此模板参数仅限于包含具有特定功能的特定类型的唯一指针的向量。例如:
/* somewhere in the code */
std::vector< std::unique_ptr< Widget > > myVec;
/* work with myVec and fill it, then call the relevant function */
func(myVec);
现在函数func
需要检索member_func
的成员函数Widget
的返回类型。请注意,Widget
也可以是不同的类型,只要它具有成员函数member_func
。
template <typename Vec>
void func(const Vec& vec) {
using ret_type = decltype(Vec::value_type::element_type::member_func()); // Doesn't work
std::vector< ret_type > local_vec;
}
我尝试过各种各样的事情,例如std::result_of
,std::invoke_result
和decltype
,但我似乎无法使其发挥作用。这甚至是可能的,如果是的话,怎么可能实现呢?
答案 0 :(得分:4)
这接近你想要的吗?
#include <vector>
#include <utility>
#include <memory>
struct Foo
{
int member_func();
};
template <typename Vec>
void func(const Vec& vec) {
using ret_type = decltype(std::declval<typename Vec::value_type>()->member_func());
std::vector< ret_type > local_vec;
}
int main()
{
std::vector<std::unique_ptr<Foo>> v;
func(v);
}
演示:https://godbolt.org/g/dJkSf1
说明:
std::declval<typename Vec::value_type>()
生成对unique_ptr的引用(必须在未评估的上下文中使用)。然后我们采用调用generated_reference->member_function()
的decltype。
这与vec[0]->member_func()
确实,我们可以这样写:
template <typename Vec>
void func(const Vec& vec) {
using ret_type = decltype(vec.at(0)->member_func());
std::vector< ret_type > local_vec;
}
哪个可能更具表现力和通用性(Vec
现在可能是类似矢量的任何类型,并且类似指针的类型为Foo
)
此外,我们越接近推论,我们的func
函数就越通用:
#include <vector>
#include <utility>
#include <memory>
#include <set>
#include <iterator>
struct Foo
{
int member_func();
};
template <typename Vec>
void func(const Vec& vec) {
using ret_type = decltype((*std::begin(vec))->member_func());
std::vector< ret_type > local_vec;
}
int main()
{
std::vector<std::unique_ptr<Foo>> v;
func(v);
func(std::array<std::unique_ptr<Foo>, 10> { });
Foo* foos[] = { nullptr, nullptr };
func(foos);
func(std::set<std::shared_ptr<Foo>, std::owner_less<>> {});
}
此代码假定Foo::member_func
的return_type不是引用类型。
如果有可能,我们需要决定是否使用元编程来:
a)将引用类型转换为std :: reference_wrapper,因此它们可以存储在向量中,或者
b)使用std::decay
将引用类型转换为基本类型,这将导致复制。