Skip to content

array.hpp: 各种问题 #2

Description

@frederick-vs-ja

目前存在的问题

  1. 用户代码的宏定义使用了保留标识符,导致程序非良构不要求诊断。
    #ifndef __ARRAY_HPP__
    #define __ARRAY_HPP__
  2. 使用了不可移植的标准库实现内部头文件。
    #include<xutility>
  3. 依赖了标准库实现的扩展(std::exception 的额外构造函数),而这一扩展甚至是不遵从的。
    i. 从标准角度来看重写 what 还是有必要的。
    struct out_of_range :std::exception
    {
    out_of_range(const std::string& s) :exception(s.data()) {}//没必要重写what方法,无所谓,看需求即可,不知道怎么写就看源码实现
    };
  4. 同上,用户代码的函数声明使用了保留标识符。
    [[noreturn]] void _Xran() const {
  5. 用户代码依赖了标准库的名为保留标识符的内部函数。同样可能导致程序非良构不要求诊断,虽然目前标准在这里有些不清晰。
    std::_Xout_of_range("invalid array<T, 0> subscript");
  6. to_array_impl 应该有 mylib:: 前缀,否则可以触发 ADL 并找到其他人声明的 to_array_impl 函数。

    c-plus-plus/array/array.hpp

    Lines 283 to 292 in 02de6c5

    template <class T, std::size_t N>
    constexpr mylib::array<std::remove_cv_t<T>, N> to_array(T(&& a)[N])
    {
    return to_array_impl(std::move(a), std::make_index_sequence<N>{});
    }
    template<typename T, std::size_t N>
    constexpr mylib::array<std::remove_cv_t<T>, N>to_array(T(&a)[N]) {
    return to_array_impl(std::move(a), std::make_index_sequence<N>{});
    }

待实现的部分

  1. get 应该还有另外 3 个重载。

    c-plus-plus/array/array.hpp

    Lines 271 to 274 in 02de6c5

    template<std::size_t N, typename Ty>
    decltype(auto) get(const Ty& a) {
    return a[N];
    }
  2. 目前缺少 array 的比较运算符。
    i. 个人认为实现为隐藏友元为好,不应照搬标准库。

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions