在函数参数中使用限定名称 [英] Use of qualified name in function parameter
问题描述
根据C ++标准,函数参数的名称由 declarator-id 解析, declarator-id 也可以是限定名。这意味着,以下代码是完全有效的(如果我已经理解标准中的相关部分正确):
template<类T>
struct Sample
{
int fun(int T :: count); // T :: count是限定变量名
};
我的问题基本上是,为什么会有人写这样的代码?在什么情况下,使用限定名(在函数参数列表中)是有利的?
p>
看来我理解的部分不正确。代替上面的代码,我们可以编写以下代码(根据C ++标准):
template< class T&
struct sample
{
void fun(int arr [T :: count]);
};
gcc-4.3.4 完美编译。但是,我不是完全满意,因为T :: count不是一个参数了(我猜)。
它无效。语法允许任意的声明符,但8.3.5p8表示
标识符可以选择作为参数名提供的
;如果
存在于函数定义
(8.4)中,它命名一个参数(有时
称为正式参数)
编辑另一个句法约束声明符限制的引用(8.3p1,[dcl.meaning]):
每个声明器只包含一个
declarator-id;它命名声明的标识符
。
a declarator-id的id表达式应该是一个简单的
标识符,除了一些特殊函数(12.3,12.4,
13.5)的声明
和模板的声明专业或部分
专业(14.7)。除了成员函数(9.3)
或静态数据成员(9.4)或嵌套
类(9.7)的
定义外,declarator-id
它的类,
定义或显式实例化
的函数,变量或类
在
命名空间之外的命名空间的成员,或者之前的
的定义在其
命名空间之外声明明确的
专业化,或者是
另一个类或命名空间(11.4)的成员的
friend函数的声明。
因此在参数声明中,不能使用限定名。
/ strong>:在编辑的形式中,函数参数类型衰减到 int *
,即使在测试之前是否 T :: count
实际存在并且是一个整数常量。如果您想要一个示例,其中这种签名中的限定名称会有意义,请考虑
template< class T&
struct sample
{
void fun(int S = T :: count);
};
当 fun
编译器需要确定默认参数,如果 T
没有成员 count
转换为 int
。
According to the C++ Standard, function parameter's name is parsed by a declarator-id, and a declarator-id can also be a qualified name. That means, the following code is perfectly valid (if I've understood the relevant sections from the Standard correctly):
template<class T>
struct Sample
{
int fun(int T::count); //T::count is qualified variable name
};
My question basically is, why would anyone write such code? In what situations, the use of qualified name (in function parameter-list) can be advantageous?
EDIT:
It seems I understood the sections incorrectly. Instead of the above code, we can probably write the following code (as per the C++ standard):
template<class T>
struct sample
{
void fun(int arr[T::count]);
};
gcc-4.3.4 compiles it perfectly. But then, I'm not totally satisfied, because T::count is not a parameter anymore (I guess).
It's invalid. The syntax allows arbitrary declarators, but 8.3.5p8 says
An identifier can optionally be provided as a parameter name; if present in a function definition (8.4), it names a parameter (sometimes called "formal argument")
Edit Another quote which syntactically constraints declarators (8.3p1, [dcl.meaning]):
Each declarator contains exactly one declarator-id; it names the identifier that is declared. The id-expression of a declarator-id shall be a simple identifier except for the declaration of some special functions (12.3, 12.4, 13.5) and for the declaration of template specializations or partial specializations (14.7). A declarator-id shall not be qualified except for the definition of a member function (9.3) or static data member (9.4) or nested class (9.7) outside of its class, the definition or explicit instantiation of a function, variable or class member of a namespace outside of its namespace, or the definition of a previously declared explicit specialization outside of its namespace, or the declaration of a friend function that is a member of another class or namespace (11.4).
So in a parameter declaration, you must not use qualified names.
Edit: In the edited form, the function parameter type decays to an int*
, even before a test is being made whether T::count
actually exists and is an integer constant. If you want an example where a qualified name in such a signature would do something meaningful, consider
template<class T>
struct sample
{
void fun(int S=T::count);
};
When fun
gets called without parameter, the compiler needs to determine the default argument, which then fails if T
does not have a count
member, or that cannot be converted to int
.
这篇关于在函数参数中使用限定名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!