简体   繁体   English

Visual C ++初始化与gcc和clang的不一致

[英]Visual C++ initialization inconsistence with gcc and clang

The following piece of code prints 0 compiled with vc++ and prints 1 compiled with g++ or clang++ : 下面的代码打印0vc ++ 编译打印1g ++clang ++编译

#include <iostream>
#include <vector>

struct S
{
    S() = default;

    std::vector<int> v{0};        
};

int main()
{
    std::vector<S> s{{}};

    std::cout << s.front().v.size() << std::endl;
}

Is it a bug in vc++ ? 这是vc ++中的错误吗?

If a user-defined constructor is provided ( S() {}; instead of S() = default; ) vc++ starts to print 1 , too . 如果提供了用户定义的构造函数( S() {};而不是S() = default;vc ++也开始打印1

In reading the Standard (C++11 n3485), 12.6.2/9 states that: 在阅读标准(C ++ 11 n3485)时, 12.6.2/9指出:

If a given non-static data member has both a brace-or-equal-initializer and a mem-initializer , the initialization specified by the mem-initializer is performed, and the non-static data member's brace-or-equal-initializer is ignored. 如果给定的非静态数据成员同时具有brace-or-equal-initializermem-initializer ,则执行mem-initializer指定mem-initializer ,并且非静态数据成员的brace-or-equal-initializer值为忽略。

So the question becomes if the default , ie the implicitly defined constructor contains a mem-initializer or not. 所以问题就变成了default ,即隐式定义的构造函数是否包含mem-initializer

Section 12.1/6 states: 12.1/6节规定:

The implicitly-defined default constructor performs the set of initializations of the class that would be performed by a user-written default constructor for that class with no ctor-initializer (12.6.2) and an empty compound-statement . 隐式定义的默认构造函数执行该类的初始化集,该初始化集将由该用户编写的默认构造函数执行,该类没有ctor-initializer (12.6.2)和空compound-statement

This would mean the implicitly generated (default) constructor has no mem-initializer s and should indeed use the in-class initializer (the brace-or-equal-initializer in the above quote). 这意味着隐式生成的(默认)构造函数没有mem-initializer并且确实应该使用类内初始化程序(上面引用中的brace-or-equal-initializer )。

MSVC is wrong here (no surprise there, really). MSVC在这里是错误的(真的没有惊喜)。

This is not an answer but a comment requiring code. 这不是答案,而是需要代码的评论。

The following illustrates the compiler differences more clearly, I think: 下面更清楚地说明了编译器的差异,我认为:

#include <iostream>
#include <vector>

struct S
{
    S() = default;

    std::vector<int> v = std::vector<int>(13);
};

int main()
{
    std::vector<S> s{{}};
    std::cout << s.front().v.size() << std::endl;
}

Here g++ MinGW 5.1.0 reports 13 items, while MSVC 2015 update 2 reports 0 items. 这里g ++ MinGW 5.1.0报告13项,而MSVC 2015更新2报告0项。

Ie g++ uses the initializer specified in the class, while MSVC uses the one specified in the declaration of s . 即g ++使用类中指定的初始化程序,而MSVC使用s声明中指定的初始化程序。 To me that looks like a problem with g++. 对我来说,这看起来像g ++的问题。 But I'm not sure: the only sure thing is that both can't be right. 但我不确定:唯一确定的是两者都不对。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 clang 和 gcc 中的视觉 C++ __declspec(属性声明属性)是否有替代方案? - Is there an alternative for visual C++ __declspec (property declaration attribute) in clang and gcc? 成员指针的C ++类内初始化使MSVC失败(但GCC / Clang起作用) - C++ in-class initialization of pointer-to-member makes MSVC to fail (but GCC/Clang work) 如何在clang(或gcc)中解码C ++名称? - How to demangle a C++ name in clang (or gcc)? 从std :: map派生的类不能在Visual C ++上编译(但可以在gcc和clang上编译) - Class derived from std::map does not compile on Visual C++ (but compiles on gcc and clang) Gcc和clang之间的C ++不一致 - C++ inconsistency between gcc and clang C ++ MSVC / GCC / Clang编译器错误 - C++ MSVC/GCC/Clang compilers bug Visual C ++和gcc一样强大吗? - Is Visual C++ as powerful as gcc? Why visual c++ (latest) and gcc 12.1 accepted hiding this init capture for lambda, while clang 14.0.0 not? (c++20) - Why visual c++ (latest) and gcc 12.1 accepted hiding this init capture for lambda, while clang 14.0.0 not? (c++20) 可以通过C和C ++方法优化LTO for gcc或clang - Can LTO for gcc or clang optimize across C and C++ methods clang上的C ++模板定义失败,适用于GCC - C++ template definition fails on clang, works on GCC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM