简体   繁体   English

C++ 没有模板的模板专业化<>

[英]C++ template specialization without template<>

I have two versions of program.我有两个版本的程序。 First:第一的:

template<class T>
void f(T i, T j) = delete;

template<>
void f(int i, int j) {
    cout << i << j << endl;
};

int main()
{
    f(1.5, 2);

    return 0;
}

And second:第二:

template<class T>
void f(T i, T j) = delete;

void f(int i, int j) {
    cout << i << j << endl;
};

int main()
{
    f(1.5, 2);

    return 0;
}

The first version won't compile because 1.5 and 2 have different types.第一个版本无法编译,因为1.52有不同的类型。 In the second version I removed template<> so 1.5 will be converted to 1 and program will run successfully.在第二个版本中,我删除了template<> ,因此1.5将转换为1 ,程序将成功运行。 So, when we remove template<> , is it still template specialization, or is it something else?那么,当我们删除template<>时,它仍然是模板特化,还是别的什么? Is there any difference besides implicit type casting?除了隐式类型转换之外还有什么区别吗? Is it useful?有用吗?

For a (unqualified) function invocation as f(1.5, 2) in your code the compiler builds a set of candidate functions which contains regular functions and functions generated from templates.对于代码中作为f(1.5, 2)的(非限定)function 调用,编译器构建一组候选函数,其中包含常规函数和从模板生成的函数。 A function template is not a function until the template arguments have been resolved and substituted.在解析并替换模板 arguments 之前,function 模板不是 function。 See overload resolution for full details.有关完整详细信息,请参阅 重载解决方案。

f is function template and it cannot deduce T from arguments of type double and int . f是 function 模板,它不能从类型为doubleint的 arguments 推导出T The template specialization isn't considered because template argument deduction failed.不考虑模板特化,因为模板参数推导失败。 The viable set of functions is empty and the compilation fails to compile the call.可行的函数集为空,编译无法编译调用。

You can resolve T ambiguity for the compiler if you call it as f<int> but such a call considers f templates only (because you explicitly specify the template parameter).如果将编译器称为f<int> ,则可以为编译器解决T歧义,但这样的调用仅考虑f模板(因为您明确指定了模板参数)。

If you make the full specialization of f not a template, it becomes an regular function. In this case the template argument deduction still fails as before, but now there is another function f and the set of candidate functions contains that one function f .如果你使f的完全特化不是模板,它就会变成常规的 function。在这种情况下,模板参数推导仍然像以前一样失败,但现在有另一个 function f并且候选函数集包含那个 function f It can be called with the supplied arguments because double is implicitly converted to int .可以使用提供的 arguments 调用它,因为double被隐式转换为int

For functions parameters whose types aren't deduced from the argument types (ie the parameter type is not a template parameter or an explicitly specified template parameter) the compiler considers implicit conversion of argument types to function parameter types and double to int is in implicit conversion and that's why the call to overloaded function f succeeds.对于类型不是从参数类型推导的函数参数(即参数类型不是模板参数或显式指定的模板参数),编译器认为参数类型隐式转换为 function 参数类型, doubleint是隐式转换这就是对重载 function f的调用成功的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM