Genxers
Genxers

Reputation: 59

Gcc 7.2 c++17 constexpr

I try to implement a constexpr stack only for understand constexpr. I get a compile error from the following code that i don't understand:

  1. If I correctly understand constexpr does not imply const
  2. It compiles the init-list constexpr constructor that contains calls to push
  3. It compiles the lambda spop that perform a pop

What am I missing?

live example

g++ prog.cc -Wall -Wextra -I/opt/wandbox/boost-1.65.0/gcc-7.2.0/include -std=gnu++1z

#include <array>
#include <stdexcept>
#include <type_traits>

namespace ds {
    template <typename T, std::size_t N>
    class array_stack final {
    public:
        using value_type = T;
        using reference = value_type&;
        using const_reference = value_type const&;
        using size_type = std::size_t;

        constexpr bool empty () const
        {
          return items_ == size_type{0};
        }

        constexpr bool full () const
        {
          return top_item_ == N;
        }

        constexpr size_type size () const
        {
          return items_;
        }

        constexpr reference top () &
        {
          if (empty())
            throw std::logic_error{"Attempting top() on empty stack"};

          return array_[top_item_ - 1];
        }

        constexpr const_reference top () const&
        {
          if (empty())
            throw std::logic_error{"Attempting top() on empty stack"};

          return array_[top_item_ - 1];
        }

        constexpr void push (value_type const& value)
        {
          if (full())
            throw std::logic_error{"Attempting push() on full stack"};

          array_[top_item_] = value;
          top_item_++;
          items_++;
        }

        constexpr void push (value_type&& value)
        {
          if (full())
            throw std::logic_error{"Attempting push() on full stack"};

          array_[top_item_] = std::move(value);
          top_item_++;
          items_++;
        }

        constexpr void pop ()
        {
          if (empty())
            throw std::logic_error{"Attempting pop() on empty stack"};

          top_item_--;
          items_--;
        }

        constexpr void clear ()
        {
          items_ = size_type{0};
          top_item_ = size_type{0};
        }

        constexpr array_stack ()
            : items_{size_type{0}}, top_item_{size_type{0}}, array_{}
        {}

        constexpr array_stack (std::initializer_list<value_type> values) : array_stack ()
        {
          for (auto const& v : values)
            push(v);
        }

        constexpr array_stack (array_stack const& rhs) : array_stack ()
        {
          array_ = rhs.array_;
          items_ = rhs.items_;
          top_item_ = rhs.top_item_;
        }

        constexpr array_stack (array_stack&& rhs)
            : items_ {rhs.items_}, top_item_ {rhs.top_item_}, array_ {std::move(rhs.array_)}
        {
          rhs.items_ = size_type{0};
          rhs.top_item_ = size_type{0};
        }

        constexpr array_stack& operator= (array_stack rhs)
        {
          array_ = std::move(rhs.array_);
          items_ = std::move(rhs.items_);
          top_item_ = std::move(rhs.top_item_);
          return *this;
        }

        ~array_stack () = default;

        void swap (array_stack& rhs) noexcept(std::is_nothrow_swappable_v<value_type>)
        {
          using std::swap;
          swap(items_, rhs.items_);
          swap(top_item_, rhs.top_item_);
          swap(array_, rhs.array_);
        }

    private:
        size_type items_;
        size_type top_item_;
        std::array<value_type, N> array_;
    };

    template <typename T, std::size_t N>
    void swap (array_stack<T, N>& lhs, array_stack<T, N>& rhs) noexcept(noexcept(lhs.swap(rhs)))
    {
        lhs.swap(rhs);
    }
}

constexpr bool f()
{
  constexpr ds::array_stack <int, 10> dstack{0,1,2,3,4,5,6,7,8,9};
  constexpr ds::array_stack <int, 10> dstack2{dstack};
  constexpr auto spop =[](auto s){ s.pop(); return s.size(); };
  static_assert(dstack.size() == 10);
  static_assert(!dstack.empty());
  static_assert(dstack.full());
  static_assert(dstack.top() == 9);
  static_assert(dstack2.size() == 10);
  static_assert(spop(dstack) == 9);
  dstack2.pop();
  return true;
}


int main()
{
  constexpr ds::array_stack <int, 10> cstack;
  static_assert(cstack.size() == 0);
  static_assert(cstack.empty());
  static_assert(!cstack.full());

  static_assert(f());

  return 0;
}

I get this error (i understand what it means but why?)

prog.cc: In function 'constexpr bool f()':
prog.cc:147:15: error: passing 'const ds::array_stack<int, 10>' as 'this' argument discards qualifiers [-fpermissive]
   dstack2.pop();
               ^
prog.cc:66:24: note:   in call to 'constexpr void ds::array_stack<T, N>::pop() [with T = int; long unsigned int N = 10]'
         constexpr void pop ()
                        ^~~

Upvotes: 1

Views: 720

Answers (2)

cpplearner
cpplearner

Reputation: 15868

[expr.const]/2:

An expression e is a core constant expression unless the evaluation of e, following the rules of the abstract machine, would evaluate one of the following expressions:

  • [...]
  • modification of an object unless it is applied to a non-volatile lvalue of literal type that refers to a non-volatile object whose lifetime began within the evaluation of e;

Upvotes: 1

Barry
Barry

Reputation: 302987

  1. If I correctly understand constexpr does not imply const

No. Objects declared constexpr are indeed const. That's why dstack2.pop() is ill-formed - the very boring and C++03 reason that you're calling a non-const member function on a const object.

Once you remove the dstack2.pop() line, everything compiles.

  1. It compiles the init-list constexpr constructor that contains calls to push
  2. It compiles the lambda spop that perform a pop

In both of these cases, you're allowed to modify the object. In the first case, you're still in the constructor - so modifications are fine, the object is never const during construction (otherwise you couldn't construct it). In the lambda case, the argument isn't const - it's just auto.

Upvotes: 3

Related Questions