Template argument deduction for references as arguments

旧街凉风 提交于 2019-12-10 19:59:56

问题


I am trying to profoundly understand Template Argument Deduction. One point I am not understanding is, how I should apply the rules in the standard here for the types A and P for the following case (there is sadly no example on cppreference.com, see below the relevant section)

template<typename T>
void foo(T t);

void call_with_reference(int& r) {
    foo(r)
}
  • P is no reference typ:
    which gives P := T
  • A := int&

-> Match P and A which gives: T is deduced to int&

which is cleary wrong. Where is the rule in the standard that says references from A are removed? A non-confusing, unambiguous clear answer would be very much appreciated.

Relevant Section:


回答1:


A is the type of an expression. Expression type is described by [expr.type]/1:

If an expression initially has the type “reference to T” ([dcl.ref], [dcl.init.ref]), the type is adjusted to T.

So here A is int.

This expression is an lvalue but that will not play any role since P is not a reference.



来源:https://stackoverflow.com/questions/56548276/template-argument-deduction-for-references-as-arguments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!