mercredi 31 mars 2021

Weird error for implicit constructor on std::array implementation

#include "array.hpp"

int main() {
    constexpr array<int, 5> a1 = { { 2, 4, 6, 8 } };
}

Here is my code ^

I searched up, and it was said for an implicit constructor to do { { blah, blah, blah } } like this, but still didn't work. I also just did a uniform brace initialization syntax (a1 = { 2, 4, 6, 8 }) but didn't work.

Error:

main.cpp: In function ‘int main()’:
main.cpp:5:57: error: could not convert ‘2’ from ‘<brace-enclosed initializer list>’ to ‘const xstl::array<int, 5>’
    5 |     constexpr xstl::array<int, 5> a1 = { { 2, 4, 6, 8 } };
      |                                                         ^
      |                                                         |
      |                                                         <brace-enclosed initializer list>
main.cpp:6:57: error: could not convert ‘1’ from ‘<brace-enclosed initializer list>’ to ‘const xstl::array<int, 5>’
    6 |     constexpr xstl::array<int, 5> a2 = { { 1, 3, 5, 7 } };
      |                                                         ^
      |                                                         |
      |                                                         <brace-enclosed initializer list>

I don't understand the error because the constructor is implicit?

Why is this aggregate erroring? Why is the std::array implementation working while this implementation does not?

Here is the implementation, not too much:

#ifndef HEADER_ARRAY_HPP
#define HEADER_ARRAY_HPP

#include <cstddef>
#include <iterator>
#include <stdexcept>

template <class T, std::size_t N>
struct array {
    using value_type = T;
    using size_type = std::size_t;
    using difference_type = std::ptrdiff_t;
    using reference = value_type&;
    using const_reference = const value_type&;
    using pointer = value_type*;
    using const_pointer = const value_type*;
    using iterator = value_type*;
    using const_iterator = const value_type*;
    using reverse_iterator = std::reverse_iterator<iterator>;
    using const_reverse_iterator = std::reverse_iterator<const_iterator>;

    const_reference at(size_type pos) const {
        return pos < size() ? Data[pos] : throw std::out_of_range("");
    }

    const_reference operator[](size_type pos) const {
        return Data[pos];
    }

    reference front() {
        return *begin();
    }

    const_reference front() const {
        return *begin();
    }

    reference back() {
        return *std::prev(end());
    }

    const_reference back() const {
        return *std::prev(end());
    }

    T *data() noexcept {
        return Data;
    }

    const T *data() const noexcept {
        return Data;
    }

    iterator begin() noexcept {
        return Data;
    }

    const_iterator begin() const noexcept {
        return Data;
    }

    const_iterator cbegin() const noexcept {
        return Data;
    }

    iterator end() noexcept {
        return Data + size();
    }

    const_iterator end() const noexcept {
        return Data + size();
    }

    const_iterator cend() const noexcept {
        return Data + size();
    }

    reverse_iterator rbegin() noexcept {
        return reverse_iterator(end());
    }

    const_reverse_iterator rbegin() const noexcept {
        return const_reverse_iterator(end());
    }

    const_reverse_iterator crbegin() const noexcept {
        return const_reverse_iterator(end());
    }

    reverse_iterator rend() noexcept {
        return reverse_iterator(begin());
    }

    const_reverse_iterator rend() const noexcept {
        return const_reverse_iterator(begin());
    }

    const_reverse_iterator crend() const noexcept {
        return const_reverse_iterator(begin());
    }

    constexpr bool empty() const noexcept {
        return begin() == end();
    }

    constexpr size_type size() const noexcept {
        return N;
    }

    constexpr size_type max_size() const noexcept {
        return N;
    }

    void fill(const T& value) {
        size_type i = 0;

        for (; i < size(); i++) {
            Data[i] = value;
        }
    }

    void swap(array& other) noexcept {
        size_type i;

        for (i = 0; i < other.size(); i++) {
            other.Data[i] = Data[i];
        }

        for (i = 0; i < size(); i++) {
            Data[i] = other.Data[i];
        }
    }

private:
    T Data[N];
};

#endif // HEADER_ARRAY_HPP

Aucun commentaire:

Enregistrer un commentaire