函数模板的瞬间地址可以作为函数指针传递给某个函数吗?
Can an address of an instant of a template to a function be passed as a pointer to function to some function?
我有以下代码,编译没有任何问题
#include<iostream>
#include<vector>
template<typename T>
void compare(const std::vector<T> &v1, const std::vector<T> &v2, void(*func)(const int&,const int&,const size_t&))
{
size_t minLen{v1.size() < v2.size() ? v1.size() : v2.size()};
for(size_t index{}; index<minLen; index++)
func(v1[index],v2[index],index);
}
template<typename T>
void matcher(const T& val1,const T& val2,const size_t& index)
{
if(val1==val2) std::cout<<"v1 and v2 are equal at index "<<index<<"\n";
}
int main()
{
std::vector v1={1,5,-9,-8};
std::vector v2={1,5,-9,-80,45};
compare(v1,v2,&(matcher<int>));
return 0;
}
现在我有以下问题:compare(v1,v2,&(matcher<int>));
是否等同于 compare(v1,v2,&matcher);
以及为什么
编辑
当我在匹配器中删除 index
var 之前的 const
时,编译器显示以下错误
temp.cpp(85): error C2664: 'void compare1<int>(const std::vector<int,std::allocator<int>> &,const std::vector<int,std::allocator<int>> &,void (__cdecl *)(const int &,const int &,const size_t &))': cannot convert argument 3 from 'void (__cdecl *)(const T &,const T &,size_t &)' to 'void (__cdecl *)(const int &,const int &,const size_t &)'
temp.cpp(85): note: None of the functions with this name in scope match the target type
temp.cpp(64): note: see declaration of 'compare1'
编译器说
'void (__cdecl *)(const T &,const T &,size_t &)' to 'void (__cdecl *)(const int &,const int &,const size_t &)'
没说
'void (__cdecl *)(const int &,const int &,size_t &)' to 'void (__cdecl *)(const int &,const int &,const size_t &)'
matcher
是一个模板,用于生成普通的C++函数。 matcher<int>
是一个完全正常的 C++ 函数。所以matcher<int>
确实是一个void(*func)(const int&,const int&,const size_t&)
,但是不,matcher
不是一个函数,它是一个模板。
我认为您无法将 matcher
模板传递给 compare
,但是 HTNW proved me wrong。显然,编译器足够聪明,可以在将模板强制转换为函数函数指针时推断出模板参数。整洁的。这种模板推导通常不适用于 class 个模板。
compare(v1,v2,&matcher);
有效是因为 template argument deduction.
Template argument deduction is used when taking an address of a overload set, which includes function templates.
和
If the function name names a function template, then, first, template argument deduction is done, and if it succeeds, it produces a single template specialization which is added to the set of overloads to consider.
compare
的第3个函数参数类型为void(*func)(const int&,const int&,const size_t&)
,传&matcher
时,matcher
的模板参数T
推导为int
, 效果与显式指定模板参数为 matcher<int>
.
相同
顺便说一句:打印出什么样的消息取决于编译器的实现;例如clang给出不同的。
我有以下代码,编译没有任何问题
#include<iostream>
#include<vector>
template<typename T>
void compare(const std::vector<T> &v1, const std::vector<T> &v2, void(*func)(const int&,const int&,const size_t&))
{
size_t minLen{v1.size() < v2.size() ? v1.size() : v2.size()};
for(size_t index{}; index<minLen; index++)
func(v1[index],v2[index],index);
}
template<typename T>
void matcher(const T& val1,const T& val2,const size_t& index)
{
if(val1==val2) std::cout<<"v1 and v2 are equal at index "<<index<<"\n";
}
int main()
{
std::vector v1={1,5,-9,-8};
std::vector v2={1,5,-9,-80,45};
compare(v1,v2,&(matcher<int>));
return 0;
}
现在我有以下问题:compare(v1,v2,&(matcher<int>));
是否等同于 compare(v1,v2,&matcher);
以及为什么
编辑
当我在匹配器中删除 index
var 之前的 const
时,编译器显示以下错误
temp.cpp(85): error C2664: 'void compare1<int>(const std::vector<int,std::allocator<int>> &,const std::vector<int,std::allocator<int>> &,void (__cdecl *)(const int &,const int &,const size_t &))': cannot convert argument 3 from 'void (__cdecl *)(const T &,const T &,size_t &)' to 'void (__cdecl *)(const int &,const int &,const size_t &)'
temp.cpp(85): note: None of the functions with this name in scope match the target type
temp.cpp(64): note: see declaration of 'compare1'
编译器说
'void (__cdecl *)(const T &,const T &,size_t &)' to 'void (__cdecl *)(const int &,const int &,const size_t &)'
没说
'void (__cdecl *)(const int &,const int &,size_t &)' to 'void (__cdecl *)(const int &,const int &,const size_t &)'
matcher
是一个模板,用于生成普通的C++函数。 matcher<int>
是一个完全正常的 C++ 函数。所以matcher<int>
确实是一个void(*func)(const int&,const int&,const size_t&)
,但是不,matcher
不是一个函数,它是一个模板。
我认为您无法将 matcher
模板传递给 compare
,但是 HTNW proved me wrong。显然,编译器足够聪明,可以在将模板强制转换为函数函数指针时推断出模板参数。整洁的。这种模板推导通常不适用于 class 个模板。
compare(v1,v2,&matcher);
有效是因为 template argument deduction.
Template argument deduction is used when taking an address of a overload set, which includes function templates.
和
If the function name names a function template, then, first, template argument deduction is done, and if it succeeds, it produces a single template specialization which is added to the set of overloads to consider.
compare
的第3个函数参数类型为void(*func)(const int&,const int&,const size_t&)
,传&matcher
时,matcher
的模板参数T
推导为int
, 效果与显式指定模板参数为 matcher<int>
.
顺便说一句:打印出什么样的消息取决于编译器的实现;例如clang给出不同的。