[英]GCC and Clang disagree about C++17 constexpr lambda captures
[英]Gcc 7.2 c++17 constexpr
我嘗試實現constexpr堆棧只是為了了解constexpr。 我從以下我不理解的代碼中得到了一個編譯錯誤:
我想念什么?
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;
}
我收到此錯誤(我理解這是什么意思,但是為什么?)
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 ()
^~~
- 如果我正確理解constexpr並不意味着const
否。聲明為constexpr
對象確實是const
。 這就是dstack2.pop()
的原因-非常無聊且C ++ 03的原因是您要在const
對象上調用非const
成員函數。
刪除dstack2.pop()
行后,所有內容都會編譯。
- 它編譯包含調用請求的init-list constexpr構造函數。
- 它編譯執行流行音樂的lambda流行音樂
在這兩種情況下,都允許您修改對象。 在第一種情況下,您仍在構造函數中-進行修改就可以了,該對象在構造期間永遠不會是const
(否則無法構造它)。 在lambda情況下,參數不是const
只是auto
。
表達式e是核心常量表達式,除非按照抽象機的規則對e求值將對以下表達式之一求值:
- [...]
- 修改對象,除非將其應用於文字類型的非易失性左值,該值是指壽命在e的求值內開始的非易失性對象;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.