ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

具有不同范围的其他相同名称的C名称解析规则

2019-08-28 01:08:18  阅读:198  来源: 互联网

标签:name-lookup c language-lawyer


我意识到以下是可怕的风格,但为了争论,假设我有以下代码:

struct parent
{
   virtual ~parent() {}
};

struct child : public parent
{
   child() {}
   virtual ~child() {}
};

struct anotherClass
{
   static parent& anyName;
};

child anyName; // create an instance of 'child'
parent& anotherClass::anyName = anyName; // create a parent-class ref to the child object

当我使用anyName初始化上面的anotherClass :: anyName引用时,anyName是我用它初始化它?子类对象,还是自己? (最后一行中最后一个anyName引用哪个实体?它是不明确的吗?)在C规范中哪里可以解决这样的问题?

(顺便说一句,这个问题与我前几分钟发布的other question完全无关.)

解决方法:

与自己.

与您的其他问题不同,这个问题可以通过一些示例代码来解决.我将你的问题中的代码插入在线编译器(clang)……

http://rextester.com/EDW85421

编译器的响应非常明确:

warning: reference ‘anyName’ is not yet bound to a value when used within its own initialization

(尝试将您的孩子注释掉anyName; line;请注意,编译结果不会更改,因为编译器无论如何都找不到该对象.)

适用的C标准规则在[basic.lookup.unqual]中:

A name used in the definition of a static data member of class X (after the qualified-id of the static member) is looked up as if the name was used in a member function of X.

来自[basic.scope.pdecl]

The point of declaration for a name is immediately after its complete declarator and before its initializer (if any), except as noted below.

这两个规则一起确保初始化程序对anyName的非限定查找找到anotherClass :: anyName.

标签:name-lookup,c,language-lawyer
来源: https://codeday.me/bug/20190828/1746452.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有