|
752 | 752 | Function parameters cannot be declared \tcode{constexpr}.\exitnote |
753 | 753 | \enterexample |
754 | 754 | \begin{codeblock} |
755 | | -constexpr int square(int x); // OK: declaration |
| 755 | +constexpr void square(int &x); // OK: declaration |
756 | 756 | constexpr int bufsz = 1024; // OK: definition |
757 | 757 | constexpr struct pixel { // error: \tcode{pixel} is a type |
758 | 758 | int x; |
759 | 759 | int y; |
760 | 760 | constexpr pixel(int); // OK: declaration |
761 | 761 | }; |
762 | 762 | constexpr pixel::pixel(int a) |
763 | | - : x(square(a)), y(square(a)) // OK: definition |
764 | | - { } |
| 763 | + : x(a), y(x) // OK: definition |
| 764 | + { square(x); } |
765 | 765 | constexpr pixel small(2); // error: \tcode{square} not defined, so \tcode{small(2)} |
766 | 766 | // not constant~(\ref{expr.const}) so \tcode{constexpr} not satisfied |
767 | 767 |
|
768 | | -constexpr int square(int x) { // OK: definition |
769 | | - return x * x; |
| 768 | +constexpr void square(int &x) { // OK: definition |
| 769 | + x *= x; |
770 | 770 | } |
771 | 771 | constexpr pixel large(4); // OK: \tcode{square} defined |
772 | 772 | int next(constexpr int x) { // error: not for parameters |
|
806 | 806 | its \grammarterm{function-body} shall be |
807 | 807 | \tcode{= delete}, \tcode{= default}, or |
808 | 808 | a \grammarterm{compound-statement} |
809 | | -that contains only |
| 809 | +that does not contain |
810 | 810 |
|
811 | 811 | \begin{itemize} |
812 | | -\item null statements, |
813 | | -\item \grammarterm{static_assert-declaration}{s} |
814 | | -\item \tcode{typedef} declarations and \grammarterm{alias-declaration}{s} that |
815 | | -do not define classes or enumerations, |
816 | | -\item \grammarterm{using-declaration}{s}, |
817 | | -\item \grammarterm{using-directive}{s}, |
818 | | -\item and exactly one return statement. |
| 812 | +\item an \grammarterm{asm-definition}, |
| 813 | +\item a \tcode{goto} statement, |
| 814 | +\item a \grammarterm{try-block}, or |
| 815 | +\item a definition of a variable |
| 816 | +of non-literal type or |
| 817 | +of static or thread storage duration or |
| 818 | +for which no initialization is performed. |
819 | 819 | \end{itemize} |
820 | 820 |
|
821 | 821 | \end{itemize} |
|
826 | 826 | { return x * x; } // OK |
827 | 827 | constexpr long long_max() |
828 | 828 | { return 2147483647; } // OK |
829 | | -constexpr int abs(int x) |
830 | | - { return x < 0 ? -x : x; } // OK |
831 | | -constexpr void f(int x) // error: return type is \tcode{void} |
832 | | - { /* ... */ } |
| 829 | +constexpr int abs(int x) { |
| 830 | + if (x < 0) |
| 831 | + x = -x; |
| 832 | + return x; // OK |
| 833 | +} |
| 834 | +constexpr int first(int n) { |
| 835 | + static int value = n; // error: variable has static storage duration |
| 836 | + return value; |
| 837 | +} |
| 838 | +constexpr int uninit() { |
| 839 | + int a; // error: variable is uninitialized |
| 840 | + return a; |
| 841 | +} |
833 | 842 | constexpr int prev(int x) |
834 | | - { return --x; } // error: use of decrement |
835 | | -constexpr int g(int x, int n) { // error: body not just ``return expr'' |
| 843 | + { return --x; } // OK |
| 844 | +constexpr int g(int x, int n) { // OK |
836 | 845 | int r = 1; |
837 | 846 | while (--n > 0) r *= x; |
838 | 847 | return r; |
|
862 | 871 | \begin{itemize} |
863 | 872 | \item |
864 | 873 | either its \grammarterm{function-body} shall be \tcode{= default}, or the \grammarterm{compound-statement} of its \grammarterm{function-body} |
865 | | -shall contain only |
866 | | - |
867 | | -\begin{itemize} |
868 | | -\item null statements, |
869 | | -\item \grammarterm{static_assert-declaration}{s} |
870 | | -\item \tcode{typedef} declarations and \grammarterm{alias-declaration}{s} that |
871 | | -do not define classes or enumerations, |
872 | | -\item \grammarterm{using-declaration}{s}, |
873 | | -\item and \grammarterm{using-directive}{s}; |
874 | | -\end{itemize} |
| 874 | +shall satisfy the constraints for a \grammarterm{function-body} of a |
| 875 | +\tcode{constexpr} function; |
875 | 876 |
|
876 | 877 | \item |
877 | 878 | every non-variant non-static data member and base class sub-object |
|
884 | 885 |
|
885 | 886 | \item |
886 | 887 | every constructor involved in initializing non-static |
887 | | -data members and base class sub-objects shall be a \tcode{constexpr} constructor; |
888 | | - |
889 | | -\item |
890 | | -every \grammarterm{assignment-expression} that is an \grammarterm{initializer-clause} |
891 | | -appearing directly or indirectly within a \grammarterm{brace-or-equal-initializer} |
892 | | -for a non-static data member that is not named by a \grammarterm{mem-initializer-id} |
893 | | -shall be a constant expression. |
| 888 | +data members and base class sub-objects shall be a \tcode{constexpr} constructor. |
894 | 889 | \end{itemize} |
895 | 890 |
|
896 | 891 | \enterexample |
897 | 892 | \begin{codeblock} |
898 | 893 | struct Length { |
899 | 894 | explicit constexpr Length(int i = 0) : val(i) { } |
900 | 895 | private: |
901 | | - int val; |
| 896 | + int val; |
902 | 897 | }; |
903 | 898 | \end{codeblock} |
904 | 899 | \exitexample |
905 | 900 |
|
906 | 901 | \pnum |
907 | | -\indexdefn{function invocation substitution}\term{Function invocation substitution} |
908 | | -for a call of a \tcode{constexpr} function or |
909 | | -of a \tcode{constexpr} constructor means: |
910 | | - |
911 | | -\begin{itemize} |
912 | | - |
913 | | -\item |
914 | | -implicitly converting each argument to the corresponding parameter |
915 | | -type as if by copy-initialization,\footnote{The resulting converted |
916 | | -value will include an lvalue-to-rvalue conversion~(\ref{conv.lval}) if |
917 | | -the corresponding copy-initialization requires one.} |
918 | | - |
919 | | -\item |
920 | | -substituting that converted expression for each use of the corresponding parameter in the |
921 | | -\grammarterm{function-body}, |
922 | | - |
923 | | -\item |
924 | | -in a member function, substituting for each use of |
925 | | -\tcode{this}~(\ref{class.this}) a prvalue pointer whose value is the |
926 | | -address of the object for which the member function is called, and |
927 | | - |
928 | | -\item |
929 | | -in a \tcode{constexpr} function, implicitly converting the resulting |
930 | | -returned expression or \grammarterm{braced-init-list} to the |
931 | | -return type of the function as if by copy-initialization. |
932 | | -\end{itemize} |
933 | | - |
934 | | -Such substitution does not change the |
935 | | -meaning. \enterexample |
936 | | -\begin{codeblock} |
937 | | -constexpr int f(void *) { return 0; } |
938 | | -constexpr int f(...) { return 1; } |
939 | | -constexpr int g1() { return f(0); } // calls \tcode{f(void *)} |
940 | | -constexpr int g2(int n) { return f(n); } // calls \tcode{f(...)} even for \tcode{n == 0} |
941 | | -constexpr int g3(int n) { return f(n*0); } // calls \tcode{f(...)} |
942 | | - |
943 | | -namespace N { |
944 | | - constexpr int c = 5; |
945 | | - constexpr int h() { return c; } |
946 | | -} |
947 | | -constexpr int c = 0; |
948 | | -constexpr int g4() { return N::h(); } // value is \tcode{5}, \tcode{c} is not looked up again after the substitution |
949 | | -\end{codeblock} |
950 | | -\exitexample |
951 | | - |
952 | | -For a non-template, non-defaulted \tcode{constexpr} function, if no function |
953 | | -argument values exist such that the function |
954 | | -invocation substitution would produce a constant expression~(\ref{expr.const}), the |
955 | | -program is ill-formed; no diagnostic required. For a non-template, |
956 | | -non-defaulted, non-inheriting \tcode{constexpr} constructor, if no |
957 | | -argument values exist such that after function invocation substitution, every constructor |
958 | | -call and full-expression in the \grammarterm{mem-initializer}{s} would be a constant |
959 | | -expression (including conversions), the program is ill-formed; no diagnostic required. |
| 902 | +For a non-template, non-defaulted |
| 903 | +\tcode{constexpr} function or a non-template, non-defaulted, non-inheriting |
| 904 | +\tcode{constexpr} constructor, if no argument values exist such that |
| 905 | +an invocation of the function or constructor could be an evaluated subexpression of a core |
| 906 | +constant expression~(\ref{expr.const}), the program is ill-formed; no diagnostic |
| 907 | +required. |
960 | 908 | \enterexample |
961 | 909 | \begin{codeblock} |
962 | 910 | constexpr int f(bool b) |
|
998 | 946 | function can appear in a constant expression. |
999 | 947 |
|
1000 | 948 | \pnum |
1001 | | -A \tcode{constexpr} specifier for a non-static member |
1002 | | -function that is not a constructor declares that member function to be |
1003 | | -\tcode{const}~(\ref{class.mfct.non-static}). |
1004 | | -\enternote The \tcode{constexpr} specifier has no |
1005 | | -other effect on the function type.\exitnote |
1006 | | -The keyword \tcode{const} is ignored if it appears in the \grammarterm{cv-qualifier-seq} |
1007 | | -of the function declarator of the declaration of such a member function. |
| 949 | +The \tcode{constexpr} specifier has no |
| 950 | +effect on the type of a \tcode{constexpr} function or a \tcode{constexpr} constructor. |
1008 | 951 | The class of |
1009 | | -which that function is a member shall be a literal |
| 952 | +which a \tcode{constexpr} function is a member shall be a literal |
1010 | 953 | type~(\ref{basic.types}). \enterexample |
1011 | 954 | \begin{codeblock} |
1012 | 955 | class debug_flag { |
1013 | 956 | public: |
1014 | 957 | explicit debug_flag(bool); |
1015 | | - constexpr bool is_on(); // error: \tcode{debug_flag} not |
| 958 | + constexpr bool is_on() const; // error: \tcode{debug_flag} not |
1016 | 959 | // literal type |
1017 | 960 | private: |
1018 | 961 | bool flag; |
|
1035 | 978 | that call shall be a constant expression~(\ref{expr.const}). |
1036 | 979 | Otherwise, |
1037 | 980 | or if a \tcode{constexpr} specifier is used in a reference declaration, |
1038 | | -every full-expression that appears in its initializer shall be a constant expression. Each |
| 981 | +every full-expression that appears in its initializer shall be a constant expression. \enternote Each |
1039 | 982 | implicit conversion used in converting the initializer expressions and each constructor call |
1040 | | -used for the initialization shall be one of those allowed in a constant |
1041 | | -expression~(\ref{expr.const}). |
| 983 | +used for the initialization is part of such a full-expression. \exitnote |
1042 | 984 | \enterexample |
1043 | 985 | \begin{codeblock} |
1044 | 986 | struct pixel { |
|
0 commit comments