使用函数指针作为参数匹配模板失败
Failure to Match Template Using Function Pointer as Argument
我正试图编写一个函数,该函数将对任何键的std::映射和给定的类指针进行操作,并根据类中函数的返回值创建一个新的std::映射和一个索引。本质上,是一个模板函数,用于根据映射所包含类中的函数重新索引映射。但是,我在尝试调用函数时遇到了编译器错误。
template<class AnyType, class ValueType, class FunctionType>
AssocArray<FunctionType,ValueType> reindex( const AssocArray<AnyType, ValueType>& original, FunctionType (*getterFunction)() ) {
AssocArray<FunctionType, ValueType> ret;
FunctionType index;
for(typename AssocArray<AnyType,ValueType>::const_iterator it = original.begin(); it!=original.end(); it++) {
index = ((*it->second).*getterFunction)();
ret[index] = it->second;
}
return ret;
}
调用方:
floatIndexed = reindex( intIndexed, &test::getB );
其中getB是浮点类型。
这导致编译器错误:
src/World.cpp:78:50: error: no matching function for call to ‘reindex(std::map<int, onathacar::test*>&, float (onathacar::test::*)())’
src/World.cpp:78:50: note: candidate is:
./include/Types.h:123:36: note: template<class AnyType, class ValueType, class FunctionType> std::map<PreservedType, ValueType> onathacar::reindex(const std::map<LookupType, ValueType>&, FunctionType (*)())
我尝试过不同的变体,包括使用"FunctionType(ValueType::*getterFunction)()"和将"AssocArray"更改为"AssocArray"。唯一有效的添加了第四个模板参数:
template<class AnyType, class ValueType, class FunctionType, class SomeType>
AssocArray<FunctionType,ValueType> reindex( const AssocArray<AnyType, ValueType>& original, FunctionType (SomeType::*getterFunction)() ) {
然而,这似乎可能会允许调用实际上不是ValueType成员的函数,因此我更喜欢其他选项。我甚至不确定出了什么问题,因为模板似乎匹配,至少添加了"ValueType::"。为什么调用与模板不匹配,有没有办法在没有第四个模板化类型的情况下修复它?
有关更多上下文,请参阅包含实现和调用函数的标头。
您有两个问题。首先,reindex
意味着值类型是值,但您将它们用作指针:
AssocArray<float, test*> floatIndexed;
floatIndexed = reindex( intIndexed, &test::getB );
第二个是reindex
的第二个参数需要声明为成员函数,而不是自由函数。所以reindex
应该是这样的:
template<class AnyType, class ValueType, class FunctionType>
AssocArray<FunctionType,ValueType *> reindex( const AssocArray<AnyType, ValueType *>& original, FunctionType (ValueType:: *getterFunction)() ) {
AssocArray<FunctionType, ValueType*> ret;
FunctionType index;
for(typename AssocArray<AnyType,ValueType*>::const_iterator it = original.begin(); it!=original.end(); it++) {
index = ((*it->second)->*getterFunction)();
ret[index] = it->second;
}
return ret;
}
您似乎试图将rindex()
函数与成员一起使用,但您的函数被声明为使用非成员。这行不通。这不起作用的原因是您需要一个对象来访问类的函数或数据成员。
template<class A, class B>
class X{
public:
};
class Y{
public:
int func() { return 42; }
};
template<class A, class B, class C>
X<C,B> reindex( const X<A, B>& original, C (B::*getterFunction)() ) {
X<C, B> x2;
cout << "hello" << endl;
return x2;
}
int main() {
X x1;
reindex(x1,&Y::func);
return 0;
}
这是有效的,并给出了完全有效的结果。
调用AssocArray intIndexed的问题在于,您正试图传递&test::getB作为getter函数,它假定valueType=test,其中实际值类型为test*。
- 模板参数替换失败,并且未完成隐式转换
- 具有默认模板参数的多态类的模板推导失败
- 视图中的参数推导失败:take_while
- variadic模板中的模板参数推导失败
- 当给定默认值时,为什么此模板参数推导失败
- 模板参数推导失败,函数参数/参数不匹配
- 为什么模板参数推导失败?
- 嵌套参数包扩展失败
- 模板参数的 C++ 自动模板推导失败
- 调试模板时出现问题.专门针对 Linux GCC 7、GCC 6、GCC 5、GCC 4.9 错误构建失败:模板参数 1
- 使用显式模板参数列表和 [temp.arg.explicit]/3 的函数调用的演绎失败
- 失败,出现错误 87:参数不正确.如何判断哪个参数不正确?
- 在 g++ 6.2.1 中将参数包转发到 constructor() 失败
- 当变量和参数名称匹配时,移动语义构造失败
- 作为模板参数的成员函数指针在继承的成员函数上失败,如何以及为什么?
- 模板参数推导/替换失败,lambda作为函数指针
- 调用基函数时模板参数推导失败
- 类模板参数推导失败会导致替换失败
- 折叠表达式模板参数推导/替换失败
- 结果失败或多个参数无效