考虑以下代码:
struct foo{};
int main() {
foo::foo a;
}
Run Code Online (Sandbox Code Playgroud)
我希望这个格式正确,foo通过[class]/2中的规则声明一个类型的变量(N4140,强调我的):
一个类名被插入在其中后立即宣布的范围类的名字能够被看见. 的类名也被插入到类本身的范围 ; 这被称为注入类名.出于访问检查的目的,inject-class-name被视为公共成员名称.
clang 3.6.0同意我的意见,编译上面的代码没有适用的警告-Wall -pedantic.
gcc 5.2.0 不同意,提供以下错误消息:
main.cpp: In function 'int main()':
main.cpp:5:5: error: 'foo::foo' names the constructor, not the type
foo::foo a;
Run Code Online (Sandbox Code Playgroud)
无论注入的类名称的嵌套有多深,例如,上述情况都适用foo::foo::foo::foo.
是否存在强制该构造在该上下文中被解释为构造函数的规则,或者这是一个gcc错误?或者我是否错误地解释标准报价?
最近,在将库更新到Clang 5.x时,我注意到我的代码中的一个错误,该错误先前已在Clang 4.x,GCC 5.x-6.x和MSVC 2015和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;
}
Run Code Online (Sandbox Code Playgroud)
Clang-5.x产生以下警告消息,而所有其他编译器以静默方式编译上述代码:
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;
Run Code Online (Sandbox Code Playgroud)
哪个编译器有问题?假设Clang5.x在此处具有正确的行为,并且所有其他编译器(和版本)都不正确,我是否正确.如果是这样,是否值得向MSVC和GCC提交错误报告?
我知道我可以使用指定的初始化程序初始化C99中的结构,如下所示:
typedef struct
{
char a;
char b;
int c;
} MyStruct;
MyStruct s = {.a = 1, .b = 2, .c = 3};
Run Code Online (Sandbox Code Playgroud)
(该代码在我的c ++编译器中不起作用,但(俄语)维基百科说它应该)
但是对于一些奇怪的原因,这样的代码也会编译(并按预期工作):
typedef struct
{
char a;
char b;
int c;
} MyStruct;
MyStruct arr[5];
int main(void)
{
arr[0] = (MyStruct){.a = 1, .b = 2, .c = 0x332211};
}
Run Code Online (Sandbox Code Playgroud)
我认为初始化应该只在创建对象时才有效,而不是之后.
这是正常的行为还是某种编译怪癖?它应该在C++中工作吗?花括号中究竟是什么东西?某种临时无名结构?我正在使用Keil uVision 4(并且指定的初始化程序不能在c ++模式下工作).