Constexpr Library Additions: utilities, v2
ISO/IEC JTC1 SC22 WG21 N3305 = 11-0075 - 2011-09-01
Benjamin Kosnik, bkoz@redhat.com
Daniel Krugler, daniel.kruegler@googlemail.com
Addresses: LWG 2013
Introduction
This paper details use of the ISO C++0x constexpr feature, as initially introduced in “Generalized Constant Expressions — Revision 5” (N2235) and incorporating all other subsequent changes as per ISO C++ draft N3291. It is the first revision of N3231. Several CWG issues have also influenced the language with respect to constexpr: see issues 1099, 1125, 1194, 1195, 1197, 1198, 1199 and c++-std-core postings number 17890 and 17912. For pair and tuple, see N3140. Additional implementation experience, and subsequent changes to the core language have motivated the following changes and additions to the library specification.
A list of additional places in Chapters 18 Support or 20 Utilities that can exploit the constexpr language feature is below. For the changes listed, the new text is put in place in bold and green with the older text it is replacing struckthrough in red.
01. <initializer_list>
Add the following:
- Mark class template initializer_list’s default constructor and member functions
size, begin, and end constexpr.
- Mark function templates begin and end constexpr.
02. <utility>
Add the following:
- Mark function templates forward and move constexpr.
- Mark pair copy and non-move constructors constexpr.
- Mark pair comparison operators ==, !=, >, >=, <, <= constexpr.
- Mark function template make_pair constexpr
03. <tuple>
Add the following:
- Mark tuple copy and non-move constructor constexpr.
- Make instances of function template get constexpr
Proposed wording
a) Modify 18.9 [support.initlist] as follows:
constexpr initializer_list() noexcept;
constexpr size_t size() const noexcept;
constexpr const E* begin() const noexcept;
constexpr const E* end() const noexcept;
b) Modify 18.9.1 [support.initlist.cons] as follows:
constexpr initializer_list() noexcept;
c) Modify 18.9.2 [support.initlist.access] as follows:
constexpr const E* begin() const noexcept;
constexpr const E* end() const noexcept;
constexpr size_t size() const noexcept;
d) Modify 18.9.3 [support.initlist.range] as follows:
template<class E> constexpr const E* begin(initializer_list<E> il) noexcept;
template<class E> constexpr const E* end(initializer_list<E> il) noexcept;
e) Modify 20.2 [utility] as follows:
// 20.2.3, forward/move:
template <class T>
constexpr T&& forward(typename remove_reference<T>::type& t) noexcept;
template <class T>
constexpr T&& forward(typename remove_reference<T>::type&& t) noexcept;
template <class T>
constexpr typename remove_reference<T>::type&& move(T&&) noexcept;
template <class T> typename conditional<
!is_nothrow_move_constructible<T>::value && is_copy_constructible<T>::value,
const T&, T&&>::type move_if_noexcept(T& x) noexcept;
….
// 20.3.3, pair specialized algorithms:
template <class T1, class T2>
constexpr bool operator==(const pair<T1,T2>&, const pair<T1,T2>&);
template <class T1, class T2>
constexpr bool operator< (const pair<T1,T2>&, const pair<T1,T2>&);
template <class T1, class T2>
constexpr bool operator!=(const pair<T1,T2>&, const pair<T1,T2>&);
template <class T1, class T2>
constexpr bool operator> (const pair<T1,T2>&, const pair<T1,T2>&);
template <class T1, class T2>
constexpr bool operator>=(const pair<T1,T2>&, const pair<T1,T2>&);
template <class T1, class T2>
constexpr bool operator<=(const pair<T1,T2>&, const pair<T1,T2>&);
template <class T1, class T2>
void swap(pair<T1,T2>& x, pair<T1,T2>& y)
template <class T1, class T2>
see below make_pair(T1&&, T2&&);
…
template<size_t I, class T1, class T2>
typename tuple_element<I, std::pair<T1, T2> >::type&
get(std::pair<T1, T2>&) noexcept;
template<size_t I, class T1, class T2>
typename tuple_element<I, std::pair<T1, T2> >::type&&
get(std::pair<T1, T2>&&) noexcept;
template<size_t I, class T1, class T2>
constexpr const typename tuple_element<I, std::pair<T1, T2> >::type&
get(const std::pair<T1, T2>&) noexcept;
f) Modify 20.2.3 [forward] as follows:
template <class T> constexpr T&& forward(typename remove_reference<T>::type& t) noexcept;
template <class T> constexpr T&& forward(typename remove_reference<T>::type&& t) noexcept;
template <class T> constexpr typename remove_reference<T>::type&& move(T&& t) noexcept;
g) Modify 20.3.2 [pairs.pair] as follows:
constexpr pair(const pair&) = default;
constexpr pair();
constexpr pair(const T1& x, const T2& y);
template<class U, class V> pair(U&& x, V&& y);
template<class U, class V> constexpr pair(const pair<U, V>& p);
...
constexpr pair(const T1& x, const T2& y);
4
Requires: is_copy_constructible<first_type>::value is true and is_copy_constructible<second_-
type>::value is true.
template<class U, class V> constexpr pair(const pair<U, V>& p);
9
Requires: is_constructible<first_type, const U&>::value is true and is_constructible<second_-
type, const V&>::value is true.
h) Modify 20.3.3 [pairs.spec] as follows:
template <class T1, class T2>
constexpr bool operator==(const pair<T1, T2>& x, const pair<T1, T2>& y);
template <class T1, class T2>
constexpr bool operator<(const pair<T1, T2>& x, const pair<T1, T2>& y);
template <class T1, class T2>
constexpr bool operator!=(const pair<T1, T2>& x, const pair<T1, T2>& y);
template <class T1, class T2>
constexpr bool operator>(const pair<T1, T2>& x, const pair<T1, T2>& y);
template <class T1, class T2>
constexpr bool operator>=(const pair<T1, T2>& x, const pair<T1, T2>& y);
template <class T1, class T2>
constexpr bool operator<=(const pair<T1, T2>& x, const pair<T1, T2>& y);
…
template <class T1, class T2>
constexpr pair<V1, V2> make_pair(T1&& x, T2&& y)
i) Modify 20.3.4 [pair.astuple] as follows:
template<size_t I, class T1, class T2>
typename tuple_element<I, std::pair<T1, T2> >::type&
get(pair<T1, T2>&) noexcept;
template<size_t I, class T1, class T2>
constexpr const typename tuple_element<I, std::pair<T1, T2> >::type&
get(const pair<T1, T2>&) noexcept;
j) Modify 20.4.1 [tuple.general] as follows:
template <size_t I, class... types>
constexpr typename tuple_element<I, tuple<Types...> >::type const&
get(const tuple<Types...>&) noexcept;
k) Modify 20.4.2 [tuple.tuple] as follows:
constexpr tuple();
explicit constexpr tuple(const Types&...);
template <class... UTypes>
explicit tuple(UTypes&&...)
constexpr tuple(const tuple&) = default;
tuple(tuple&&) = default;
template <class... UTypes>
constexpr tuple(const tuple<UTypes...>&);
template <class... UTypes>
tuple(tuple<UTypes...>&&) noexcept;
template <class U1, class U2>
constexpr tuple(const pair<U1, U2>&); // iff sizeof...(Types) == 2
l) Modify 20.4.2.1 [tuple.cnstr] as follows:
explicit constexpr tuple(const Types&...);
constexpr tuple(const tuple&) = default;
template <class... UTypes>
constexpr tuple(const tuple<UTypes...>&);
template <class U1, class U2>
constexpr tuple(const pair<U1, U2>&); // iff sizeof...(Types) == 2
m) Modify 20.4.2.6 [tuple.elem] as follows:
template <size_t I, class... Types>
constexpr typename tuple_element<I, tuple<Types...> >::type const&
get(const tuple<Types...>& t) noexcept;
Acknowledgments
Many thanks to Daniel Krügler, Alisdair Meredith, Jason Merrill, Paolo Carlini, and Jonathan Wakely for reviewing this document and providing assistance.