简体   繁体   English

为什么不自动推导参考模板参数?

[英]Why are reference template parameters not deduced automatically?

In the following example, I'd intuitively expect the call to inc(iref) to invoke the inc-function with T=int& , since that's the type of iref . 在下面的示例中,我很直观地期望对inc(iref)的调用将使用T=int&调用inc(iref)函数,因为这是iref的类型。 However, it seems that the & is dropped when the variable is passed to a function, thus leading to T=int in the case of both inc(i) as well as inc(iref) . 但是,似乎在将变量传递给函数时会删除& ,因此在inc(i)inc(iref)的情况下都导致T=int The behaviour I'd expect only happens when explicitly specifying the template parameter as reference. 我期望的行为仅在显式指定template参数作为引用时发生。

template<typename T> 
void inc(T t) {
    t++;
}

int main() {
    int i = 41;
    int& iref = i;

    iref++;
    std::cout << i << " " << iref << std::endl; // prints 42 42, as expected

    inc(i);
    std::cout << i << " " << iref << std::endl; // still prints 42 42, as expected

    inc(iref);
    std::cout << i << " " << iref << std::endl; // prints 42 42, instead of 43 43

    inc<int&>(iref);
    std::cout << i << " " << iref << std::endl; // now finally prints 43 43
}

So, my questions are: 因此,我的问题是:

  • Why is the reference seemingly turned into a 'bare' value when being passed via inc(iref) ? 当通过inc(iref)传递引用时,为什么引用似乎变成了“裸”值? What's the process behind it? 它背后的过程是什么?
  • Why does it work this way / what's the rationale behind that design decision? 为什么这样工作/设计决定的依据是什么? Would there be any problems or negative consequences if it worked the way I intuitively expected? 如果它按照我的直觉预期的方式工作,将会有任何问题或负面后果吗?

The reference is stripped of it's reference value because if it isn't iref is ambiguous otherwise(int& or int). 该引用被剥夺了它的引用值,因为如果不是,则iref是不明确的(int&或int)。 It is designed this way so that so that you may overload it like: 它的设计方式使您可以像这样重载它:

#include <iostream>

template<typename T> 
void inc(T& t) {
    t++;
}

int main() {
    int i = 41;
    int& iref = i;

    inc(iref);
    std::cout << iref << std::endl;
}

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

相关问题 为什么在那个例子中没有自动推导出模板参数? - Why are template arguments not deduced automatically in that example? 为什么不能推导嵌套在模板类中的枚举的模板参数? - Why can't the template parameters for enums nested in a template class be deduced? 为什么不能在函数中使用模板别名作为参数并自动推导? - Why is it not possible to use a template alias in function as parameter and to be automatically deduced? 为什么不能在`std :: reference_wrapper`s中推导出模板实例? - Why can template instances not be deduced in `std::reference_wrapper`s? 在模板模板参数的情况下,decltype(auto)可转发的参考非类型模板参数是否可转发 - Are reference non-type template parameters deduced by decltype(auto) forwardable in case of template template parameter Clang声称`成员引用基类型&#39;X&#39;不是结构或联合`,但X是带有推导参数的结构模板 - Clang claims that `member reference base type 'X' is not a structure or union`, but X is a structure template with deduced parameters 如何推导出std :: move模板参数? - How are std::move template parameters deduced? 使用引用推断模板包中的冲突类型 - Deduced conflicting types in template pack with reference 类型不推断为r值参考:为什么不呢? - Type not deduced to be r-value reference: why not? 为什么不能推导出模板参数? - Why can't the template argument be deduced?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM