为什么在推导类型时模板参数的限定符被剥离? [英] Why are qualifiers of template arguments stripped when deducing the type?

查看:112
本文介绍了为什么在推导类型时模板参数的限定符被剥离?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用Microsoft VisualStudio 2008构建一个示例程序时,我注意到了传递给模板的类型的推论。考虑这个例子:

While building a little sample program with Microsoft VisualStudio 2008 I noticed an odd thing about the deduction of types passed to templates. Consider this example:

template<class T>
void f( T v ) {
    x; // trigger a compile error
    (void)v;
}

template<class T>
void g( T v ) {
    f( v );
}

void h() {
  int i;
  g<const int &>( i );
}

使用 cl / c foo.cpp 产生编译错误(按预期)。有趣的是T模板参数的值。这是VisualStudio 2008打印的:

Compiling this example using cl /c foo.cpp yields a compile error (as intended). What's interesting is the value of the 'T' template parameter. Here's what VisualStudio 2008 prints:

mini.cpp(3) : error C2065: 'x' : undeclared identifier
        mini.cpp(9) : see reference to function template instantiation 'void f<int>(T)' being compiled
        with
        [
            T=int
        ]
        mini.cpp(14) : see reference to function template instantiation 'void g<const int&>(T)' being compiled
        with
        [
            T=const int &
        ]

注意在 g ,参数的类型是 const int& ,但是在 f 它只是 int 。显然reference-to-const部分被剥离,同时推断在实例化 f 模板时使用的类型。当调整示例以调用 f

Note how in g, the type of the argument is const int & but in f it's just int. Apparently the reference-to-const part was stripped while deducing the type to use when instantiating the f template. When adjusting the example so that f is invoked like

f<T>( v );

类型为 const int& 两个 f g 。这是为什么?这是指定的行为吗?我秘密地依赖于 v 函数参数的类型传递给 f ,但显然不是。

the type is const int & in both f and g. Why is that? Is this specified behaviour? I secretly relied on the type of the v function argument to be passed to f but apparently it's not.

推荐答案

答案是,虽然变量 v const int& ,表达式 v 是一个类型为

The answer is that although the variable v has type const int &, the expression v is an lvalue expression with type const int.

litb提供文本(5/6):如果一个表达式最初具有类型T的引用 ,8.5.3),在进行任何进一步分析之前将类型调整为T,表达式指定由引用表示的对象或函数,并且表达式是一个左值。

litb provides the text (5/6): "If an expression initially has the type "reference to T" (8.3.2, 8.5.3), the type is adjusted to "T" prior to any further analysis, the expression designates the object or function denoted by the reference, and the expression is an lvalue."

参数是以逗号分隔的列表中的一个表达式,由函数调用表达式中的括号包围(1.3.1)。所以在14.8.2.1中:

An "argument" is "an expression in the comma-separated list bounded by the parentheses in a function call expression" (1.3.1). So in 14.8.2.1:


  • 调用的相应参数类型(调用它A)是 const int

  • 如果A是cv限定类型,A类型的顶级cv限定符将被忽略类型扣除扣除过程试图找到模板参数值,这将使得推导的A与A相同(因此T是(A)=(code int

  • < int
  • "the corresponding argument type of the call (call it A)" is const int.
  • "If A is a cv-qualified type, the top-level cv-qualifiers of A's type are ignored for type deduction" (hence, int).
  • "the deduction process attempts to find template argument values that will make the deduced A identical to A" (so T is int)

这篇关于为什么在推导类型时模板参数的限定符被剥离?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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