【问题标题】:Initializing std::array with Static Storage Duration with a Parameter Pack Expansion and an Additional Value使用带有参数包扩展和附加值的静态存储持续时间初始化 std::array
【发布时间】:2015-10-24 02:13:16
【问题描述】:

在最近询问another question 时,我在使用参数包扩展后跟另一个元素 初始化std::array 时偶然发现了GCC 的一些奇怪行为。我已经与 Jarod42 in the comments there 简要讨论过这个问题,但我认为最好将其作为一个新问题提出。

例如,考虑以下代码,它应该提供一个实用程序make_array 函数,该函数接受任意数量的参数并将它们std::forwards 传递给std::array 初始化。前导标记参数选择是否应以默认构造的T(通过std::true_type 选择)终止数组(通过std::false_type 选择)。然后我创建一个整数数组,一次是静态的,一次是自动存储持续时间的。数组元素终于打印出来了。

#include <array>        // std::array
#include <cstddef>      // std::size_t
#include <iomanip>      // std::setw
#include <ios>          // std::left, std::right
#include <iostream>     // std::cout
#include <string>       // std::string
#include <type_traits>  // std::false_type, std::true_type


// This is only used for visualization.
template <typename T, std::size_t N>
void
print_array(const std::string& name, const std::array<T, N>& arr)
{
  std::cout << std::setw(20) << std::left << (name + ":") << std::right << "{";
  for (auto iter = arr.cbegin(); iter != arr.cend(); ++iter)
    std::cout << (iter != arr.cbegin() ? ", " : "") << std::setw(2) << *iter;
  std::cout << "}\n";
}

// Create a `std::array<T>` with elements constructed from the provided
// arguments.
template <typename T, typename... ArgTs>
static constexpr auto
make_array(std::false_type, ArgTs&&... args) noexcept
{
  std::array<T, sizeof...(args)> values = { { std::forward<ArgTs>(args)... } };
  return values;
}

// Create a `std::array<T>` with elements constructed from the provided
// arguments followed by a default-constructed `T`.
template <typename T, typename... ArgTs>
static constexpr auto
make_array(std::true_type, ArgTs&&... args) noexcept
{
  std::array<T, sizeof...(args) + 1> values = {
    { std::forward<ArgTs>(args)..., T {} }
  };
  return values;
}

namespace /* anonymous */
{
  const auto values_no_static = make_array<int>(std::false_type(), 1, 2, 3, 4);
  const auto values_yes_static = make_array<int>(std::true_type(), 1, 2, 3, 4);
}

int
main()
{
  const auto values_no_automatic = make_array<int>(std::false_type(), 1, 2, 3, 4);
  const auto values_yes_automatic = make_array<int>(std::true_type(), 1, 2, 3, 4);
  print_array("static yes", values_yes_static);
  print_array("static no", values_no_static);
  print_array("automatic yes", values_yes_automatic);
  print_array("automatic no", values_no_automatic);
}

我希望看到数组{1, 2, 3, 4}{1, 2, 3, 4, 0} 打印两次。这就是 Clang 所做的。然而,GCC 为具有静态存储持续时间的终止数组打印{0, 0, 0, 0, 0}。如果我用-1 替换初始化列表中的尾随T {},我会得到{0, 0, 0, 0, -1}。因此,添加尾随元素似乎会导致参数包扩展为全零。但前提是生成的 std::array 具有静态存储持续时间。

我是否调用了未定义的行为或者这是 GCC 中的错误?如果它是未定义的行为,我会感谢官方参考标准。我已经知道解决此问题的简单方法(请参阅my answer 到上述问题)并且不感兴趣避免问题。相反,我想知道代码是否格式正确,如果是,哪个编译器是正确的。

使用 GCC 完成输出:

$ g++ --version
g++ (GCC) 5.1.0
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$
$ g++ -std=c++14 -o main_gcc -Wall -Wextra -Werror -pedantic main.cxx
$
$ ./main_gcc 
static yes:         { 0,  0,  0,  0,  0}
static no:          { 1,  2,  3,  4}
automatic yes:      { 1,  2,  3,  4,  0}
automatic no:       { 1,  2,  3,  4}

使用 Clang:

$ clang --version
clang version 3.6.2 (tags/RELEASE_362/final)
Target: x86_64-unknown-linux-gnu
Thread model: posix
$
$ clang -std=c++14 -o main_clang -Wall -Wextra -Werror -pedantic main.cxx -lstdc++
$
$ ./main_clang 
static yes:         { 1,  2,  3,  4,  0}
static no:          { 1,  2,  3,  4}
automatic yes:      { 1,  2,  3,  4,  0}
automatic no:       { 1,  2,  3,  4}

【问题讨论】:

    标签: c++ g++ language-lawyer variadic-templates c++14


    【解决方案1】:

    最小复制样本:

    #include <array>
    
    constexpr auto make_array(int i)
    {
        std::array<int, 2> values = { i, 0 };
        return values;
    }
    
    constexpr auto arr = make_array(1);
    
    static_assert(arr[0] == 1, "");
    

    Demo with HEAD。至此,我们可以肯定地说没有诱发 UB。

    注意我们如何解决这个错误:

    【讨论】:

    • +1 用于简化示例。 “将0 更改为1”很有趣,因为它不会改变我原来示例中的行为。
    • 你是 GCC 开发者吗,@Columbo?你觉得我应该举报吗?
    • @5gon12eder 我不是 gcc 开发人员。是的,你一定要举报。
    【解决方案2】:

    我已将此情况报告给 GCC 开发人员。它被识别为bug 67104,并于 2015 年 8 月针对 GCC 6 进行了修复,并向后移植到了 GCC 5.3。您可以使用Columbo's answer中提供的在线编译器链接进行检查。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-06-24
      • 2020-02-17
      • 2016-11-06
      • 2019-04-15
      • 1970-01-01
      • 2012-01-01
      • 1970-01-01
      相关资源
      最近更新 更多