[英]Call of explicitly instantiated template function through conversion operator
Let us assume we have a function template which is implemented in the cpp file with help of explicit instantiation like this: 让我们假设我们有一个函数模板,它在cpp文件中实现,借助显式实例化,如下所示:
function.h function.h
template<typename T> void function(T val);
function.cpp function.cpp
#include "function.h"
template<typename T> void function(T val) { /* do something */ }
template void function<double>(double val);
We are now able to call the function in a main file that includes function.h like this: 我们现在能够在包含function.h的主文件中调用该函数,如下所示:
double val = 1.0;
function(val);
Let us further assume we have a class which is implemented like this: 让我们进一步假设我们有一个类如下所示:
data.h data.h
class Data
{
private:
double mVal;
public:
Data(double val) { mVal = val; }
operator double () { return mVal; }
};
The following code results in the linker error LNK2019: unresolved external (Visual Studio 2010): 以下代码导致链接器错误LNK2019:unresolved external(Visual Studio 2010):
Data a(1.0);
function(a);
We could use one of the following expressions to supply a to function() 我们可以使用以下表达式之一来提供一个 to function()
function<double>(a);
function(double(a));
...
but why is it not possible to just call function(a) ? 但为什么不能只调用函数(a) ? Does there exist any other solution to achieve that without explicitly instantiating function() with type Data?
如果没有使用类型Data显式实例化function() ,是否存在任何其他解决方案?
why is it not possible to just call
function(a)
?为什么不能只调用
function(a)
?
It is. 它是。 You're calling it.
你在叫它。 But remember that
function
is declared as: 但请记住,该
function
声明为:
template<typename T> void function(T val);
so template deduction will deduce function<Data>
. 因此模板推导将推导出
function<Data>
。 The template deduction doesn't know that elsewhere in the code you only have a definition for function<double>
- it just does deduction. 模板推导不知道代码中的其他地方你只有
function<double>
的定义 - 它只是做演绎。 And function<Data>
doesn't have a definition, so it fails to link. function<Data>
没有定义,因此无法链接。
Performing the explicit cast yourself (either function<double>(a)
or function(static_cast<double>(a))
) would be the best solution in my opinion. 在我看来,自己执行显式转换(
function<double>(a)
或function(static_cast<double>(a))
)将是最好的解决方案。 Explicit is nice. 明确很好。 You could additionally write a separate function with all the overloads you actually support and just forward to the function template:
您还可以使用实际支持的所有重载编写一个单独的函数,然后转发到函数模板:
void fwd_function(double v) { function(v); }
void fwd_function(foo v) { function(v); }
void fwd_function(bar v) { function(v); }
fwd_function(a); // now we call function<double> because fwd_function(double )
// is the what we're actually calling
It is not possible to call function(a)
, because then T
will be of type Data
, and not double
, even though it has that conversion operator. 不可能调用
function(a)
,因为那么T
将是Data
类型,而不是double
,即使它有转换运算符。 And because you are not explicitly defining it in the cpp
file, you get a linker error. 并且因为您没有在
cpp
文件中明确定义它,所以会出现链接器错误。
Here are some solutions you could use: 以下是您可以使用的一些解决方案:
//Call operator double() explicitly
function(a.operator double());
//Specify T
function<double>(a);
//Casting
function(static_cast<double>(a));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.