Constexpr Library Additions: complex, v2

ISO/IEC JTC1 SC22 WG21 N3302 = 11-0072 - 2011-08-20

Benjamin Kosnik, bkoz@redhat.com

Gabriel Dos Reis, gdr@integrable-solutions.net

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 N3228. 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. Additional implementation experience, and subsequent changes to the core language have motivated the following changes and additions to the library specification.

Proposed Changes

A list of additional places in 26.4 Complex numbers aka std::complex 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.

Add the following:

        - Mark primary template constructors and real() and imag() member functions         

          constexpr.

        - Mark complex operators == and != constexpr.

        - Mark function templates real() and imag()  constexpr.

Proposed Wording

a) Modify 26.4.1 [complex.syn] as follows:

template<class T>

 constexpr bool operator==(const complex<T>&, const complex<T>&);

template<class T>

 constexpr bool operator==(const complex<T>&, const T&);

template<class T>

 constexpr bool operator==(const T&, const complex<T>&);

template<class T>

 constexpr bool operator!=(const complex<T>&, const complex<T>&);

template<class T>

 constexpr bool operator!=(const complex<T>&, const T&);

template<class T>

 constexpr bool operator!=(const T&, const complex<T>&);

template<class T> constexpr T real(const complex<T>&);

template<class T> constexpr T imag(const complex<T>&);

b) Modify 26.4.2 [complex] as follows:

constexpr complex(const T& re = T(), const T& im = T());

constexpr complex(const complex&);

template<class X> constexpr complex(const complex<X>&);

constexpr T real() const;

void real(T);

constexpr T imag() const;

void imag(T);

c) Modify 26.4.6 [complex.ops] as follows:

template<class T> constexpr bool

 operator==(const complex<T>& lhs, const complex<T>& rhs);

template<class T> constexpr bool

 operator==(const complex<T>& lhs, const T& rhs);

template<class T> constexpr bool

 operator==(const T& lhs, const complex<T>& rhs);

template<class T> constexpr bool

 operator!=(const complex<T>& lhs, const complex<T>& rhs);

template<class T> constexpr bool

 operator!=(const complex<T>& lhs, const T& rhs);

template<class T> constexpr bool

 operator!=(const T& lhs, const complex<T>& rhs);

d) Modify 26.4.7 [complex.value.ops] as follows:

template<class T> constexpr T real(const complex<T>& x);

template<class T> constexpr T imag(const complex<T>& x);

 

Acknowledgments

Many thanks to Jason Merrill, Paolo Carlini, and Jonathan Wakely for reviewing this document and providing assistance.