简体   繁体   English

通过转换运算符调用显式实例化的模板函数

[英]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.

相关问题 将显式实例化的函数模板与转换匹配 - Matching explicitly instantiated function template with conversion 未定义的对显式实例化模板函数的引用 - Undefined reference to explicitly instantiated template function Visual C ++ - 显式调用基本类型的转换运算符 - Visual C++ - call conversion operator on a primitive type explicitly 模板类中的模板转换运算符 - 到函数指针 - Template conversion operator in template class - to function pointer 是否可以调用模板化的强制转换操作符显式指定模板参数? - Is it possible to call the templated cast operator explicitly specifying template parameters? 明确实例化的 std::less、std::greater 和类似的对象不提供到 function 指针的转换是否有客观原因? - Is there an objective reason why the explicitly instantiated std::less, std::greater and similar offer no conversion to function pointer? 显式实例化模板类的显式实例化模板方法 - Explicitly instantiate template method of explicitly instantiated template class 显式实例化模板方法中的编译错误 - Compilation error in explicitly instantiated template methods 显式实例化类模板中的自动构造函数 - Automatic constructor in explicitly instantiated class template 从 dll 导入显式实例化的模板 class - Importing explicitly instantiated template class from dll
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM