Declare std::array members with attribute const [PR101831].

Resolves:
PR libstdc++/101831 - Spurious maybe-uninitialized warning on std::array::size

libstdc++-v3/ChangeLog:

	PR libstdc++/101831
	* include/std/array (begin): Declare const member function attribute
	const.
	(end, rbegin, rend, size, max_size, empty, data): Same.
	* testsuite/23_containers/array/capacity/empty.cc: Add test cases.
	* testsuite/23_containers/array/capacity/max_size.cc: Same.
	* testsuite/23_containers/array/capacity/size.cc: Same.
	* testsuite/23_containers/array/iterators/begin_end.cc: New test.
This commit is contained in:
Martin Sebor 2022-02-01 17:19:11 -07:00
parent 07a6c52c4c
commit 756eabacfc
5 changed files with 101 additions and 8 deletions

View File

@ -127,7 +127,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
{ std::swap_ranges(begin(), end(), __other.begin()); }
// Iterators.
[[__nodiscard__]]
[[__gnu__::__const__, __nodiscard__]]
_GLIBCXX17_CONSTEXPR iterator
begin() noexcept
{ return iterator(data()); }
@ -137,7 +137,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
begin() const noexcept
{ return const_iterator(data()); }
[[__nodiscard__]]
[[__gnu__::__const__, __nodiscard__]]
_GLIBCXX17_CONSTEXPR iterator
end() noexcept
{ return iterator(data() + _Nm); }
@ -147,7 +147,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
end() const noexcept
{ return const_iterator(data() + _Nm); }
[[__nodiscard__]]
[[__gnu__::__const__, __nodiscard__]]
_GLIBCXX17_CONSTEXPR reverse_iterator
rbegin() noexcept
{ return reverse_iterator(end()); }
@ -157,7 +157,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
rbegin() const noexcept
{ return const_reverse_iterator(end()); }
[[__nodiscard__]]
[[__gnu__::__const__, __nodiscard__]]
_GLIBCXX17_CONSTEXPR reverse_iterator
rend() noexcept
{ return reverse_iterator(begin()); }
@ -188,15 +188,15 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
{ return const_reverse_iterator(begin()); }
// Capacity.
[[__nodiscard__]]
[[__gnu__::__const__, __nodiscard__]]
constexpr size_type
size() const noexcept { return _Nm; }
[[__nodiscard__]]
[[__gnu__::__const__, __nodiscard__]]
constexpr size_type
max_size() const noexcept { return _Nm; }
[[__nodiscard__]]
[[__gnu__::__const__, __nodiscard__]]
constexpr bool
empty() const noexcept { return size() == 0; }
@ -278,7 +278,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
: _AT_Type::_S_ref(_M_elems, 0);
}
[[__nodiscard__]]
[[__gnu__::__const__, __nodiscard__]]
_GLIBCXX17_CONSTEXPR pointer
data() noexcept
{ return _AT_Type::_S_ptr(_M_elems); }

View File

@ -40,8 +40,26 @@ test01()
}
}
#pragma GCC push_options
#pragma GCC optimize "0"
void
test02()
{
{
const size_t len = 3;
typedef std::array<int, len> array_type;
array_type a;
VERIFY( a.empty() == false ); // { dg-bogus "-Wmaybe-uninitialized"
}
}
#pragma GCC pop_options
int main()
{
test01();
test02();
return 0;
}

View File

@ -40,8 +40,26 @@ test01()
}
}
#pragma GCC push_options
#pragma GCC optimize "0"
void
test02()
{
{
const size_t len = 3;
typedef std::array<int, len> array_type;
array_type a;
VERIFY( a.max_size() == len ); // { dg-bogus "-Wmaybe-uninitialized"
}
}
#pragma GCC pop_options
int main()
{
test01();
test02();
return 0;
}

View File

@ -40,8 +40,26 @@ test01()
}
}
#pragma GCC push_options
#pragma GCC optimize "0"
void
test02()
{
{
const size_t len = 3;
typedef std::array<int, len> array_type;
array_type a;
VERIFY( a.size() == len ); // { dg-bogus "-Wmaybe-uninitialized"
}
}
#pragma GCC pop_options
int main()
{
test01();
test02();
return 0;
}

View File

@ -0,0 +1,39 @@
// { dg-do compile { target c++11 } }
#include <array>
#pragma GCC push_options
#pragma GCC optimize "0"
extern void
sink (const void*, ...);
void
test01()
{
{
const std::size_t len = 1;
typedef std::array<int, len> array_type;
typedef array_type::iterator iterator;;
array_type a;
iterator b = a.begin(); // { dg-bogus "-Wmaybe-uninitialized" }
iterator e = a.end(); // { dg-bogus "-Wmaybe-uninitialized" }
sink(&b, &e);
}
{
const std::size_t len = 3;
typedef std::array<int, len> array_type;
typedef array_type::reverse_iterator reverse_iterator;
array_type a;
reverse_iterator b = a.rbegin(); // { dg-bogus "-Wmaybe-uninitialized" }
reverse_iterator e = a.rend(); // { dg-bogus "-Wmaybe-uninitialized" }
sink(&b, &e);
}
}
#pragma GCC pop_options