类名注入和构造函数 [英] Class Name Injection and Constructors

查看:233
本文介绍了类名注入和构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,在将库更新为Clang 5.x时,我注意到我的代码中有一个错误,该错误以前是在Clang 4.x,GCC 5.x-6.x和MSVC 2015和2017上编译的.

Recently, upon updating a library to Clang 5.x, I noticed a bug in my code that had compiled previously on Clang 4.x, GCC 5.x-6.x, and MSVC 2015 and 2017.

#include <iostream>
#include <typeinfo>
#include <vector>

int main()
{
    using a = typename std::vector<int>::vector;
    std::cout << typeid(a).name() << std::endl;
    return 0;
}

Clang-5.x产生以下警告消息,而所有其他编译器均以静默方式编译上述代码:

Clang-5.x produces the following warning message, while all other compilers silently compile the above code:

a.cpp:7:42: warning: ISO C++ specifies that qualified reference to 'vector' is a
  constructor name rather than a type in this context, despite preceding
  'typename' keyword [-Winjected-class-name]
using a = typename std::vector<int>::vector;

哪个编译器有错误?我假设Clang5.x在这里具有正确的行为,并且所有其他编译器(和版本)都不正确,这是正确的吗?如果是这样,是否值得将错误报告提交给MSVC和GCC?

Which compiler is buggy? Am I correct in assuming that Clang5.x has the correct behavior here, and all the other compilers (and versions) are incorrect. If so, is this worth submitting bug reports to MSVC and GCC?

推荐答案

Clang-5非常正确.在 [class.qual]/2 :

Clang-5 is very much correct. Over at [class.qual]/2:

在不忽略函数名称的查找中, 嵌套名称说明符 指定C类:

In a lookup in which function names are not ignored and the nested-name-specifier nominates a class C:

  • 如果在嵌套名称说明符之后指定的名称在C中查找,则为C的注入类名称
  • ...
  • if the name specified after the nested-name-specifier, when looked up in C, is the injected-class-name of C
  • ...

该名称被认为是为类C的构造函数命名的.

关于问题的另一部分.是的,提交错误报告绝对值得.应当鼓励IMO遵守标准(或至少对其进行更多诊断).

As for the other part of the question. Yes, it's definitely worth it to submit bug reports. Standard compliance (or at least more diagnostics towards it) are to be encouraged IMO.

这篇关于类名注入和构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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