【发布时间】: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