引用模板作为参数的模板参数推导 [英] Template argument deduction for references as arguments

查看:135
本文介绍了引用模板作为参数的模板参数推导的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图深刻理解模板参数推导。
我不了解的一点是,如何应用标准
中的规则

解决方案

A 是表达式的类型。表达式类型由 [expr.type] / 1 描述:


如果表达式最初的类型为对T的引用([dcl.ref],[dcl.init.ref]),则类型调整为T。


所以这里 A int



此表达式是 lvalue ,但不会播放角色,因为 P 不是参考。


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)
}

-> 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:

解决方案

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.

这篇关于引用模板作为参数的模板参数推导的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆