[英]Passing member function address of template class/struct as a function parameter
My aim is to get custom container method that returns another container with same elements but converted to a different type by passing a convertor member function address.我的目标是获得自定义容器方法,该方法返回另一个具有相同元素但通过传递转换器成员函数地址转换为不同类型的容器。 Example in int main() below describes it in simplest way.
下面的 int main() 示例以最简单的方式描述了它。
My problem is that I can't figure out proper declaration of such function parameter (if it is possible).我的问题是我无法弄清楚此类函数参数的正确声明(如果可能的话)。
PS: I am using MSVC compiler. PS:我正在使用 MSVC 编译器。
template<typename container_type>
struct Container
{
...
template
<typename convert_type = container_type,
typename = std::enable_if<!std::is_fundamental<container_type>::value>::type,
typename = std::enable_if<!std::is_pointer<container_type>::value>::type>
Container<convert_type> apply(convert_type (const container_type::*function)() const) const
// error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
// note: see reference to class template instantiation 'Container<container_type>' being compiled
// error C2091: function returns function
{
Container<convert_type> result();
result.reserve(this->size());
for (const auto& i : *this)
{
result.push_back(i.*function());
}
return result;
}
}
int main()
{
Container<std::string> c = { "a", "b", "c" };
Container<const char*> a = c.apply(&std::string::c_str);
}
To fix the missing return type, you may use要修复缺少的返回类型,您可以使用
Container<convert_type> apply(convert_type (container_type::*function)() const) const
You'll have to fix the syntax to make the function call though.不过,您必须修复语法才能进行函数调用。
result.push_back((i.*function)());
See a simpler example that demonstrates the idea at https://ideone.com/REb7Yu .在https://ideone.com/REb7Yu 上看到一个更简单的例子来演示这个想法。
Figured it out.弄清楚了。
Remarks from R Sahu are correct! R Sahu 的评论是正确的! But there is also a 3rd mistake so I should add another answer to cover it completely.
但是还有第三个错误,所以我应该添加另一个答案来完全覆盖它。
1) There is unnecessary const before container_type. 1) 在container_type 前没有不必要的const。 That is the reason of "error C2091: function returns function".
这就是“错误 C2091:函数返回函数”的原因。
2) Wrong syntax for call of member function by pointer. 2) 指针调用成员函数的语法错误。
3) If one makes for example Container<SomeClass*> then not only apply() method will not be available, but that code will not compile as it is not SFINAE sufficient. 3) 例如,如果创建 Container<SomeClass*> ,那么不仅 apply() 方法将不可用,而且该代码将无法编译,因为它不是 SFINAE 足够的。 The function pointer type should be template argument as well.
函数指针类型也应该是模板参数。 So proper declaration of that function template should be:
所以该函数模板的正确声明应该是:
template
<typename convert_type,
typename = std::enable_if<!std::is_fundamental<container_type>::value>::type,
typename = std::enable_if<!std::is_pointer<container_type>::value>::type,
typename func_type = convert_type (type::*)() const>
Container<convert_type> apply(func_type function) const
{
...
result.push_back((i.*function)());
...
}
Now that works.现在可以了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.