为什么span的数组和std::array构造函数与其容器构造函数不同
Why are span's array and std::array constructors different from its container constructors
我一直在 Godbolt 上使用 clang trunk 和 libc++ 进行最新的std::span
规范,发现一些构造函数令人困惑。
特别是,我发现来自普通旧数组和std::array
的构造函数与其他容器不同。
例如,以下代码似乎要编译:
std::vector<int*> v = {nullptr, nullptr};
std::span<const int* const> s{v};
但是,这不会:
std::array<int*, 2> a = {nullptr, nullptr};
std::span<const int* const> s{a};
这似乎与 cppreference.com 上描述构造函数的方式一致,我只是在努力理解为什么会这样。有人能说出什么光吗?
这似乎是一个疏忽。数组构造函数当前指定为:
template<size_t N> constexpr span(array<value_type, N>& arr) noexcept;
template<size_t N> constexpr span(const array<value_type, N>& arr) noexcept;
但可能应该指定为:
template<class T, size_t N>
requires std::convertible_to<T(*)[], ElementType(*)[]>
constexpr span(array<T, N>& arr) noexcept;
template<class T, size_t N>
requires std::convertible_to<const T(*)[], ElementType(*)[]>
constexpr span(const array<T, N>& arr) noexcept;
这将使您的示例编译,因为它是安全的。我提交了LWG问题。现在是 LWG 3255。
措辞已经在[span.cons]/11 中指定了此约束:
template<size_t N> constexpr span(element_type (&arr)[N]) noexcept; template<size_t N> constexpr span(array<value_type, N>& arr) noexcept; template<size_t N> constexpr span(const array<value_type, N>& arr) noexcept;
约束条件:
extent == dynamic_extent || N == extent
是true
,并且remove_pointer_t<decltype(data(arr))>(*)[]
可转换为ElementType(*)[]
。
因此,我们已经有了正确的约束。只是在这些情况下,data(arr)
实际上并不依赖于这些情况,因此约束很容易得到满足。我们只需要制作这些模板。
相关文章:
- "error: no matching function for call to"构造函数错误
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 无法推断类中的类型,构造函数采用 std::array
- C++如何在类构造函数中实例化 std::array?
- 我正在使用dev c ++,但收到错误(C++98'array'必须由构造函数初始化)
- 当 T 不可默认构造时,构造函数初始值设定项列表中 std::array<T,N> 的初始化
- 在构造函数中初始化私有 std::array 成员
- 为什么span的数组和std::array构造函数与其容器构造函数不同
- Picky STD :: String Char Array构造函数
- 简化冗余std :: array初始化,当时没有constexpr构造函数
- 无法创建将 std::array<T, n> 作为类成员的构造函数
- 使用字符串文本初始化构造函数中的 std::array<char,x> 成员。海湾合作委员会错误?
- ARRAY[T, SIZE]合适的默认构造函数可用
- 如何为 std::array 的列表初始化构造函数编写包装器?
- 为什么std::array不包含初始化列表构造函数
- 对std::array中的元素调用用户定义的构造函数
- 初始化的Array构造函数做什么?
- 将参数传递给容器构造函数"array-like"
- 在类的构造函数初始值设定项中填充 std::array
- 移动构造函数和 'std::array'