c++ - g++4.9 错误允许 std::vector<C_type_array>

标签 c++ c++11 stl gcc4.9

考虑以下代码:

#include <iostream>
#include <vector>
#include <array>

using namespace std;

typedef double (C_array)[10];

int main()
{ 
    std::vector<C_array> arr(10);

    // let's initialize it
    for (int i = 0; i < 10; i++)
        for (int j = 0; j < 10; j++)
            arr[i][j] = -1;

    // now make sure we did the right thing
    for (int i = 0; i < 10; i++)
    {
        for (int j = 0; j < 10; j++)
        {
            cout << arr[i][j] << " ";
        }
        cout << endl;
    }
}

我刚从@juanchopanza https://stackoverflow.com/a/25108679/3093378 那里得知这段代码不应该是合法的,因为一个普通的旧 C 风格的数组是不可分配/不可复制/可移动的。然而,即使使用 -Wall -Wextra -pedanticg++ 也会飞过代码。 clang++ 不编译它。当然,如果我尝试做类似 auto arr1 = arr; 的操作,它在 g++ 下会失败,因为它不知道如何复制 arr 进入 arr1

我在 OS X Mavericks 下使用了 macportsg++4.9。 此处的实时代码:http://goo.gl/97koLa

我的问题是:

  1. 根据标准,此代码是否非法?
  2. g++ 有那么多问题吗?我一直在寻找很多简单的例子,其中 g++ 盲目编译非法代码,最后一次是昨天 user-defined conversion operators precedence, compiles in g++ but not clang++ ,并且没有太多的努力,只是为了好玩而尝试 C++

最佳答案

您的代码不是有效的 C++03。一、表头<array>不是 C++03 标准库的一部分,但这里也不需要。二、vector对象的构造尝试调用构造函数

explicit vector(size_type n, const value_type& val = value_type(), const allocator_type& alloc = allocator_type());

但是val的初始化失败,原因和你不能写的一​​样

C_array foo = C_array();

据我所知,C++03 标准第 5.2.3 节中的第 2 段仅允许对非数组类型使用此表示法:

The expression T(), where T is a simple-type-specifier (7.1.5.2) for a non-array complete object type or the (possibly cv-qualified) void type, creates an rvalue of the specified type, which is value-initialized (8.5; no initialization is done for the void() case).

此外,g++-4.9.0 也拒绝编译代码,除非在命令行上提供 -std=c++11:

foo.cpp: In constructor ‘std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>::size_type, const value_type&, const allocator_type&) [with _Tp = double [10]; _Alloc = std::allocator<double [10]>; std::vector<_Tp, _Alloc>::size_type = long unsigned int; std::vector<_Tp, _Alloc>::value_type = double [10]; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<double [10]>]’:
foo.cpp:11:32: error: functional cast to array type ‘std::vector<double [10]>::value_type {aka double [10]}’
 std::vector<C_array> arr(10);
...

对于 C++11, vector 容器提供了一个额外的填充构造函数:

explicit vector (size_type n);

此构造函数要求模板类型是可默认构造的(请参阅第 23.3.6.2 节)。据我所知,这个要求在 C++11 中也没有得到满足(参见第 17.6.3.1 节),因为为了满足这个要求,表达式 C_array() 必须创建一个临时对象,这在 C+ 中也是无效的+11(再次参见第 5.2.3 节)。我不知道标准是否真的要求编译器拒绝代码,或者如果不满足其中一个要求但标准库的实现不需要它,是否允许编译器编译它。也许对 C++11 了解更多的人可以填补这里的空白。

除此之外,我认为尝试使用数组作为容器元素类型不是一个好主意,因为不满足其他容器要求。例如 C_array 不可复制插入,因此无法复制 vector 。

关于你的第二个问题:请随意浏览 gcc bugzilla 数据库 https://gcc.gnu.org/bugzilla/ .但是,接受无效代码也可以是故意的,例如为了不破坏遗留代码。

关于c++ - g++4.9 错误允许 std::vector<C_type_array>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25109064/

相关文章:

c++ - STL 排序不起作用

c++ - 通过 X10 接收和解码曼彻斯特码

c++ - wxWidgets 3.1.5 MSW - HiDPI 缩放问题导致控件大小不正确

c++ - 如何遍历以值为对的 map ?

c++ - 如何为枚举和特定类型专门化模板函数?

c++ - std::shared_ptr 中的计数器是原子的吗?

c++ - 我应该避免哪些 C++ 陷阱?

C++模板实例化: Avoiding long switches

c++ - 如何在std::vector::emplace_back上检测隐式转换损失的整数精度

c++ - 带有 C++11 的 Visual Studio 2008