假设&
没有过载。如何获取实例化模板函数的地址,例如std::sort<int*>
?以下内容在某些编译器上无法编译:
#include <algorithm>
int main()
{
&std::sort<int*>;
}
在MSVC v19.21上,它报告:https://godbolt.org/z/gpZCdn
error C2568: 'identifier': unable to resolve function overload
答案 0 :(得分:3)
您可以使用
&std::sort<int*>;
&std::sort<int>
不起作用,因为该类型需要可取消引用。
可以通过执行显式强制转换来解决歧义。
static_cast<void (*)(int*, int*)>(&std::sort<int*>);
答案 1 :(得分:3)
示例:
void (*func_ptr)(std::vector<int>::iterator, std::vector<int>::iterator) =
std::sort< std::vector<int>::iterator >;
std::vector<int> values;
for(int i = 99; i > 0; --i)
values.push_back(i);
func_ptr(values.begin(), values.end());
如果您真的想要int *作为迭代器类型
void (*func_ptr)(int*, int*) = std::sort<int*>;