Constexpr Library Additions: complex

 

ISO/IEC JTC1 SC22 WG21 N3228 = 10-0218 - 2010-11-24

Benjamin Kosnik, bkoz@redhat.com

Gabriel Dos Reis, gdr@integrable-solutions.net

 

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 N3126. Several CWG issues also influence 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 has 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 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>&);

 

b) Modify 26.4.2 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 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 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.