我的函数模板有以下问题。如果我有这样的功能模板,其中T
可以任ClassA
或ClassB
:如何将模板类型作为参数传递给非模板函数?
template<typename T, bool flag>
void function(T& someT){
if(flag){ /*If 'flag' is true, 'someT' MUST be of type ClassA*/
nonTemplateFunction1(someT);
}
else{ /*Else 'someT' must be of type ClassB*/
nonTemplateFunction2(someT);
}
}
而在其他一些文件我有
void nonTemplateFunction1(ClassA& a){...}
,并在另一个文件中我有
void nonTemplateFunction2(ClassB& b){...}
现在,如果我打电话给我的函数模板,编译时出现错误:
error: no matching member function for call to 'nonTemplateFunction2'
我想这是因为编译器测试了nonTemplateFunction2
与ClassA
作为参数的例子。但这绝不会发生(至少我,程序员,照顾这个)。我怎样才能让我的程序编译?有什么解决方法?
编辑:一个更精确的错误:
./tabledata.hh:371:9: error: no matching member function for call to 'bulk_insert' bi.bulk_insert(bl, aRel_Vec[j]);
../pax/bulk_insert_pax.hh:21:8: note: candidate function not viable: no known conversion from 'value_type' (aka 'NSM_Relation') to 'PAX_Relation &' for 2nd argument void bulk_insert(const BulkLoader& aBulkLoader, PAX_Relation& aRelation);
嗯,看来我必须通过然后指向我的T。不是我最喜欢的解决方案,但它应该工作 –
@ N.Weber我认为这是最快的选择,是的。 –