|
770 | 770 | The definition of a constexpr function shall satisfy the following
|
771 | 771 | requirements:
|
772 | 772 | \begin{itemize}
|
773 |
| -\item |
774 |
| -its return type (if any) shall be a literal type; |
775 |
| - |
776 |
| -\item |
777 |
| -each of its parameter types shall be a literal type; |
778 |
| - |
779 | 773 | \item
|
780 | 774 | it shall not be a coroutine\iref{dcl.fct.def.coroutine};
|
781 | 775 |
|
|
816 | 810 | \end{codeblock}
|
817 | 811 | \end{example}
|
818 | 812 |
|
819 |
| -\pnum |
820 |
| -\indextext{specifier!\idxcode{constexpr}!constructor}% |
821 |
| -The definition of a constexpr constructor |
822 |
| -whose \grammarterm{function-body} is not \tcode{= delete} |
823 |
| -shall additionally satisfy the following requirements: |
824 |
| -\begin{itemize} |
825 |
| - |
826 |
| -\item |
827 |
| -for a non-delegating constructor, every constructor selected to initialize non-static |
828 |
| -data members and base class subobjects shall be a constexpr constructor; |
829 |
| - |
830 |
| -\item |
831 |
| -for a delegating constructor, the target constructor shall be a constexpr |
832 |
| -constructor. |
833 |
| -\end{itemize} |
834 |
| - |
835 |
| -\begin{example} |
836 |
| -\begin{codeblock} |
837 |
| -struct Length { |
838 |
| - constexpr explicit Length(int i = 0) : val(i) { } |
839 |
| -private: |
840 |
| - int val; |
841 |
| -}; |
842 |
| -\end{codeblock} |
843 |
| -\end{example} |
844 |
| - |
845 |
| -\pnum |
846 |
| -The definition of a constexpr destructor |
847 |
| -whose \grammarterm{function-body} is not \tcode{= delete} |
848 |
| -shall additionally satisfy the following requirement: |
849 |
| -\begin{itemize} |
850 |
| -\item |
851 |
| - for every subobject of class type or |
852 |
| - (possibly multi-dimensional) array thereof, |
853 |
| - that class type shall have a constexpr destructor. |
854 |
| -\end{itemize} |
855 |
| - |
856 |
| -\pnum |
857 |
| -For a constexpr function or constexpr constructor |
858 |
| -that is neither defaulted nor a template, |
859 |
| -if no argument values exist such that |
860 |
| -an invocation of the function or constructor could be an evaluated subexpression of a core |
861 |
| -constant expression\iref{expr.const}, or, |
862 |
| -for a constructor, an evaluated subexpression of |
863 |
| -the initialization full-expression of some constant-initialized object\iref{basic.start.static}, |
864 |
| -the program is ill-formed, no diagnostic required. |
865 |
| -\begin{example} |
866 |
| -\begin{codeblock} |
867 |
| -constexpr int f(bool b) |
868 |
| - { return b ? throw 0 : 0; } // OK |
869 |
| -constexpr int f() { return f(true); } // ill-formed, no diagnostic required |
870 |
| - |
871 |
| -struct B { |
872 |
| - constexpr B(int x) : i(0) { } // \tcode{x} is unused |
873 |
| - int i; |
874 |
| -}; |
875 |
| - |
876 |
| -int global; |
877 |
| - |
878 |
| -struct D : B { |
879 |
| - constexpr D() : B(global) { } // ill-formed, no diagnostic required |
880 |
| - // lvalue-to-rvalue conversion on non-constant \tcode{global} |
881 |
| -}; |
882 |
| - |
883 |
| -constexpr int f(int x) { |
884 |
| - static int n = x; |
885 |
| - return n + x; // ill-formed, no diagnostic required |
886 |
| - // all calls reach the static variable declaration |
887 |
| -} |
888 |
| -\end{codeblock} |
889 |
| -\end{example} |
890 |
| - |
891 | 813 | \pnum
|
892 | 814 | If the instantiated template specialization of a constexpr function
|
893 | 815 | template
|
|
896 | 818 | function,
|
897 | 819 | that specialization is still a constexpr function,
|
898 | 820 | even though a call to such a function cannot appear in a constant
|
899 |
| -expression. If no specialization of the template would satisfy the |
900 |
| -requirements for a constexpr function |
901 |
| -when considered as a non-template function, the template is |
902 |
| -ill-formed, no diagnostic required. |
| 821 | +expression. |
903 | 822 |
|
904 | 823 | \pnum
|
905 | 824 | An invocation of a constexpr function in a given context
|
|
919 | 838 | This can indirectly cause calls to \tcode{std::is_constant_evaluated}
|
920 | 839 | within an invocation of the function to produce a different value.
|
921 | 840 | \end{note}
|
| 841 | +\begin{note} |
| 842 | +It is possible to write a constexpr function for which |
| 843 | +no invocation satisfies the requirements of a core constant expression. |
| 844 | +\end{note} |
922 | 845 |
|
923 | 846 | \pnum
|
924 | 847 | The \keyword{constexpr} and \keyword{consteval} specifiers have no
|
|
6249 | 6172 | \end{itemize}
|
6250 | 6173 |
|
6251 | 6174 | \pnum
|
6252 |
| -An explicitly-defaulted function that is not defined as deleted may be declared |
6253 |
| -\keyword{constexpr} or \keyword{consteval} only |
6254 |
| -if it is constexpr-compatible\iref{special,class.compare.default}. |
6255 | 6175 | A function explicitly defaulted on its first declaration
|
6256 | 6176 | is implicitly inline\iref{dcl.inline},
|
6257 |
| -and is implicitly constexpr\iref{dcl.constexpr} if it is constexpr-compatible. |
| 6177 | +and is implicitly constexpr\iref{dcl.constexpr} |
| 6178 | +if it satisfies the requirements for a constexpr function. |
6258 | 6179 |
|
6259 | 6180 | \pnum
|
6260 | 6181 | \begin{example}
|
6261 | 6182 | \begin{codeblock}
|
6262 | 6183 | struct S {
|
6263 |
| - constexpr S() = default; // error: implicit \tcode{S()} is not \keyword{constexpr} |
6264 | 6184 | S(int a = 0) = default; // error: default argument
|
6265 | 6185 | void operator=(const S&) = default; // error: non-matching return type
|
6266 | 6186 | ~S() noexcept(false) = default; // OK, despite mismatched exception specification
|
|
0 commit comments