Re: Implement N3642 - User-defined Literals for Standard Library Types

2013-06-01 Thread Ed Smith-Rowland

Committed the following...


2013-05-30  Ed Smith-Rowland  3dw...@verizon.net

Implement N3642 - User-defined Literals for Standard Library Types
* include/bits/parse_numbers.h: New.
* include/std/chrono: Add duration literal operators.
* include/bits/basic_string.h: Add string literal operators.
* include/Makefile.in: Add parse_numbers.h.
* include/Makefile.am: Ditto.
* testsuite/20_util/duration/literals/values.cc: New.
* testsuite/20_util/duration/literals/types.cc: New.
* testsuite/20_util/duration/requirements/typedefs_neg1.cc: Adjust.
* testsuite/20_util/duration/requirements/typedefs_neg2.cc: Adjust.
* testsuite/20_util/duration/requirements/typedefs_neg3.cc: Adjust.
* testsuite/21_strings/basic_string/literals/values.cc: New.
* testsuite/21_strings/basic_string/literals/types.cc: New.

Index: include/bits/parse_numbers.h
===
--- include/bits/parse_numbers.h(revision 0)
+++ include/bits/parse_numbers.h(working copy)
@@ -0,0 +1,417 @@
+// Components for compile-time parsing of numbers -*- C++ -*-
+
+// Copyright (C) 2013 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// Under Section 7 of GPL version 3, you are granted additional
+// permissions described in the GCC Runtime Library Exception, version
+// 3.1, as published by the Free Software Foundation.
+
+// You should have received a copy of the GNU General Public License and
+// a copy of the GCC Runtime Library Exception along with this program;
+// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
+// http://www.gnu.org/licenses/.
+
+/** @file bits/parse_numbers.h
+ *  This is an internal header file, included by other library headers.
+ *  Do not attempt to use it directly. @headername{chrono}
+ */
+
+#ifndef _PARSE_NUMBERS_H
+#define _PARSE_NUMBERS_H 1
+
+#pragma GCC system_header
+
+// From n3642.pdf except I added binary literals and digit separator '`'.
+
+#if __cplusplus  201103L
+
+namespace std _GLIBCXX_VISIBILITY(default)
+{
+_GLIBCXX_BEGIN_NAMESPACE_VERSION
+
+namespace __parse_int {
+
+  templateunsigned _Base, char _Dig
+struct _Digit;
+
+  templateunsigned _Base
+struct _Digit_Base, '0'
+{
+  static constexpr bool valid{true};
+  static constexpr unsigned value{0};
+};
+
+  templateunsigned _Base
+struct _Digit_Base, '1'
+{
+  static constexpr bool valid{true};
+  static constexpr unsigned value{1};
+};
+
+  templateunsigned _Base
+struct _Digit_Base, '2'
+{
+  static_assert(_Base  2, invalid digit);
+  static constexpr bool valid{true};
+  static constexpr unsigned value{2};
+};
+
+  templateunsigned _Base
+struct _Digit_Base, '3'
+{
+  static_assert(_Base  3, invalid digit);
+  static constexpr bool valid{true};
+  static constexpr unsigned value{3};
+};
+
+  templateunsigned _Base
+struct _Digit_Base, '4'
+{
+  static_assert(_Base  4, invalid digit);
+  static constexpr bool valid{true};
+  static constexpr unsigned value{4};
+};
+
+  templateunsigned _Base
+struct _Digit_Base, '5'
+{
+  static_assert(_Base  5, invalid digit);
+  static constexpr bool valid{true};
+  static constexpr unsigned value{5};
+};
+
+  templateunsigned _Base
+struct _Digit_Base, '6'
+{
+  static_assert(_Base  6, invalid digit);
+  static constexpr bool valid{true};
+  static constexpr unsigned value{6};
+};
+
+  templateunsigned _Base
+struct _Digit_Base, '7'
+{
+  static_assert(_Base  7, invalid digit);
+  static constexpr bool valid{true};
+  static constexpr unsigned value{7};
+};
+
+  templateunsigned _Base
+struct _Digit_Base, '8'
+{
+  static_assert(_Base  8, invalid digit);
+  static constexpr bool valid{true};
+  static constexpr unsigned value{8};
+};
+
+  templateunsigned _Base
+struct _Digit_Base, '9'
+{
+  static_assert(_Base  9, invalid digit);
+  static constexpr bool valid{true};
+  static constexpr unsigned value{9};
+};
+
+  templateunsigned _Base
+struct _Digit_Base, 'a'
+{
+  static_assert(_Base  0xa, invalid digit);
+  static constexpr bool valid{true};
+  static constexpr unsigned value{0xa};
+};
+
+  templateunsigned _Base
+struct _Digit_Base

Re: Implement N3642 - User-defined Literals for Standard Library Types

2013-06-01 Thread Paolo Carlini


Hi,

Ed Smith-Rowland 3dw...@verizon.net ha scritto:

Committed the following...

Looks like great work, thanks! I'm still in vacations, and barely reading email 
on a small screen, just wanted to point out that apparently the patch has a few 
redundant inline preceding constexpr. Could you please double check?

Thanks!
Paolo



Implement N3642 - User-defined Literals for Standard Library Types

2013-05-31 Thread Ed Smith-Rowland

Greetings,

This patch implements N3642 - User-defined literals for 
std::chrono::duration and std::basic_string

and N3660 - User-defined literals for std::complex.

User-defined literals were separated into two papers because of some 
controversy about noexcept for complex literals.
If desired, I could split the patch into two bits for the two 
proposals.  OTOH, I'm pretty sure complex literals will make it in.


The patch includes a utility for compile-time parsing of integers which 
is a modified version of that suggested in N3642.
Support for binary literals of the form 0b01010 and support for a digit 
separator are added relative to that paper.

The digit parsing is used in the chrono literals.

One bit: I would like someone to look over my treatment of namespace 
versioning and tell me if that's OK

and/or whether I need to mess with gnu-versioned-namespace.ver.

Otherwise, this builds and tests clean on x86_64-linux.

Thanks,
Ed


2013-05-30  Ed Smith-Rowland  3dw...@verizon.net

Implement N3642 - User-defined Literals for Standard Library Types
Implement N3660 - User-defined Literals for std::complex,
part 2 of UDL for Standard Library Types
* include/bits/parse_numbers.h: New.
* include/std/chrono: Add duration literal operators.
* include/bits/basic_string.h: Add string literal operators.
* include/std/complex: Add complex literal operators.
* include/Makefile.in: Add parse_numbers.h.
* include/Makefile.am: Ditto.
* testsuite/20_util/duration/literals/values.cc: New.
* testsuite/20_util/duration/literals/types.cc: New.
* testsuite/20_util/duration/requirements/typedefs_neg1.cc: Adjust.
* testsuite/20_util/duration/requirements/typedefs_neg2.cc: Adjust.
* testsuite/20_util/duration/requirements/typedefs_neg3.cc: Adjust.
* testsuite/21_strings/basic_string/literals/values.cc: New.
* testsuite/21_strings/basic_string/literals/types.cc: New.
* testsuite/26_numerics/complex/literals/types.cc: New.
* testsuite/26_numerics/complex/literals/values.cc: New.
* config/abi/pre/gnu.ver: Add literal operator symbols.
Index: include/bits/parse_numbers.h
===
--- include/bits/parse_numbers.h(revision 0)
+++ include/bits/parse_numbers.h(working copy)
@@ -0,0 +1,417 @@
+// Components for compile-time parsing of numbers -*- C++ -*-
+
+// Copyright (C) 2013 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// Under Section 7 of GPL version 3, you are granted additional
+// permissions described in the GCC Runtime Library Exception, version
+// 3.1, as published by the Free Software Foundation.
+
+// You should have received a copy of the GNU General Public License and
+// a copy of the GCC Runtime Library Exception along with this program;
+// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
+// http://www.gnu.org/licenses/.
+
+/** @file bits/parse_numbers.h
+ *  This is an internal header file, included by other library headers.
+ *  Do not attempt to use it directly. @headername{chrono}
+ */
+
+#ifndef _PARSE_NUMBERS_H
+#define _PARSE_NUMBERS_H 1
+
+#pragma GCC system_header
+
+// From n3642.pdf except I added binary literals and digit separator '`'.
+
+#if __cplusplus  201103L
+
+namespace std _GLIBCXX_VISIBILITY(default)
+{
+_GLIBCXX_BEGIN_NAMESPACE_VERSION
+
+namespace __parse_int {
+
+  templateunsigned _Base, char _Dig
+struct _Digit;
+
+  templateunsigned _Base
+struct _Digit_Base, '0'
+{
+  static constexpr bool valid{true};
+  static constexpr unsigned value{0};
+};
+
+  templateunsigned _Base
+struct _Digit_Base, '1'
+{
+  static constexpr bool valid{true};
+  static constexpr unsigned value{1};
+};
+
+  templateunsigned _Base
+struct _Digit_Base, '2'
+{
+  static_assert(_Base  2, invalid digit);
+  static constexpr bool valid{true};
+  static constexpr unsigned value{2};
+};
+
+  templateunsigned _Base
+struct _Digit_Base, '3'
+{
+  static_assert(_Base  3, invalid digit);
+  static constexpr bool valid{true};
+  static constexpr unsigned value{3};
+};
+
+  templateunsigned _Base
+struct _Digit_Base, '4'
+{
+  static_assert(_Base  4, invalid digit);
+  static constexpr bool valid{true};
+  static constexpr unsigned value{4

Re: Implement N3642 - User-defined Literals for Standard Library Types

2013-05-31 Thread Daniel Krügler
2013/5/31 Ed Smith-Rowland 3dw...@verizon.net:
 Greetings,

 This patch implements N3642 - User-defined literals for
 std::chrono::duration and std::basic_string
 and N3660 - User-defined literals for std::complex.

 N3660 was rejected during the Bristol meeting, the main reason being
the ugliness of the complex-float literal and giving it some time to
find a possible sore language solution. Is there still the idea to add
this now?

- Daniel


Re: Implement N3642 - User-defined Literals for Standard Library Types

2013-05-31 Thread Ed Smith-Rowland
 @@
+// { dg-options -std=gnu++1y }
+// { dg-do compile }
+
+// Copyright (C) 2013 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// http://www.gnu.org/licenses/.
+
+// NOTE: This makes use of the fact that we know how moveable
+// is implemented on string (via swap). If the implementation changed
+// this test may begin to fail.
+
+#include string
+#include type_traits
+
+void
+test01()
+{
+  using namespace std::literals::string_literals;
+
+  static_assert(std::is_samedecltype(Hellos), std::string::value,
+   \Hello\s is std::string);
+
+  static_assert(std::is_samedecltype(u8Hellos), std::string::value,
+   u8\Hello\s is std::string);
+
+  static_assert(std::is_samedecltype(LHellos), std::wstring::value,
+   L\Hello\s is std::wstring);
+
+  static_assert(std::is_samedecltype(uHellos), std::u16string::value,
+   u\Hello\s is std::u16string);
+
+  static_assert(std::is_samedecltype(UHellos), std::u32string::value,
+   U\Hello\s is std::u32string);
+}
Index: config/abi/pre/gnu.ver
===
--- config/abi/pre/gnu.ver  (revision 199471)
+++ config/abi/pre/gnu.ver  (working copy)
@@ -1365,6 +1365,20 @@
 # std::get_unexpected()
 _ZSt14get_unexpectedv;
 
+# std::literals::chrono_literals::operator()
+_ZNSt8literals15chrono_literalsli2ns*;
+_ZNSt8literals15chrono_literalsli2us*;
+_ZNSt8literals15chrono_literalsli2ms*;
+_ZNSt8literals15chrono_literalsli1s*;
+_ZNSt8literals15chrono_literalsli3min*;
+_ZNSt8literals15chrono_literalsli1h*;
+
+# std::literals::string_literals::operator()
+_ZNSt8literals15string_literalsli1sEPKcm;
+_ZNSt8literals15string_literalsli1sEPKDim;
+_ZNSt8literals15string_literalsli1sEPKDsm;
+_ZNSt8literals15string_literalsli1sEPKwm;
+
 } GLIBCXX_3.4.19;
 
 # Symbols in the support library (libsupc++) have their own tag.

2013-05-30  Ed Smith-Rowland  3dw...@verizon.net

Implement N3660 - User-defined Literals for std::complex,
part 2 of UDL for Standard Library Types
* include/std/complex: Add complex literal operators.
* testsuite/26_numerics/complex/literals/types.cc: New.
* testsuite/26_numerics/complex/literals/values.cc: New.
* config/abi/pre/gnu.ver: Add literal operator symbols.

2013-05-30  Ed Smith-Rowland  3dw...@verizon.net

Implement N3642 - User-defined Literals for Standard Library Types
* include/bits/parse_numbers.h: New.
* include/std/chrono: Add duration literal operators.
* include/bits/basic_string.h: Add string literal operators.
* include/Makefile.in: Add parse_numbers.h.
* include/Makefile.am: Ditto.
* testsuite/20_util/duration/literals/values.cc: New.
* testsuite/20_util/duration/literals/types.cc: New.
* testsuite/20_util/duration/requirements/typedefs_neg1.cc: Adjust.
* testsuite/20_util/duration/requirements/typedefs_neg2.cc: Adjust.
* testsuite/20_util/duration/requirements/typedefs_neg3.cc: Adjust.
* testsuite/21_strings/basic_string/literals/values.cc: New.
* testsuite/21_strings/basic_string/literals/types.cc: New.
* config/abi/pre/gnu.ver: Add literal operator symbols.
Index: include/std/complex
===
--- include/std/complex (revision 199471)
+++ include/std/complex (working copy)
@@ -1930,4 +1930,45 @@
 
 #endif  // C++11
 
+#if __cplusplus  201103L
+
+namespace std _GLIBCXX_VISIBILITY(default)
+{
+_GLIBCXX_BEGIN_NAMESPACE_VERSION
+
+inline namespace literals {
+inline namespace complex_literals {
+
+  inline constexpr std::complexfloat
+  operator i_f(long double __num)
+  { return std::complexfloat{0.0F, static_castfloat(__num)}; }
+
+  inline constexpr std::complexfloat
+  operator i_f(unsigned long long __num)
+  { return std::complexfloat{0.0F, static_castfloat(__num)}; }
+
+  inline constexpr std::complexdouble
+  operator i(long double __num)
+  { return std::complexdouble{0.0, static_castdouble(__num)}; }
+
+  inline constexpr std::complexdouble
+  operator i(unsigned long long __num)
+  { return std::complexdouble{0.0, static_castdouble(__num)}; }
+
+  inline constexpr std::complexlong double

Re: Re: Implement N3642 - User-defined Literals for Standard Library Types

2013-05-31 Thread 3dw4rd
 
 
 
On 05/31/13, Ed Smith-Rowland3dw...@verizon.net wrote:
 

 ...
  1. Put the precision first in upper case. As a matter of style I 
 prefer 123456L to 123456l for normal literals anyway. Also, the 
 precision snuggles next to the number - then you modify it. That seems 
 logical to me. Also, if we involve decimal in this someday, those 
 literals *have* to be all caps (for the old literals). It seems using 
 lower case precision indicators for decimal would be inconsistent.

I must withdraw the comment about decimal literals needing to be all caps.  
That's wrong.  The letters need to be all uppercase *or* all lowercase.  You 
can't have mixed case.

I'll shut up now and post this somewhere else.
 
Ed



Re: Implement N3642 - User-defined Literals for Standard Library Types

2013-05-31 Thread Jonathan Wakely
On 31 May 2013 15:15, Ed Smith-Rowland wrote:
 Greetings,

 This patch implements N3642 - User-defined literals for
 std::chrono::duration and std::basic_string
 and N3660 - User-defined literals for std::complex.

Great, thanks!

 User-defined literals were separated into two papers because of some
 controversy about noexcept for complex literals.
 If desired, I could split the patch into two bits for the two proposals.
 OTOH, I'm pretty sure complex literals will make it in.

I'm not so sure, and if they do they might use if as the suffix
rather than i_f, so please split this patch so the non-complex parts
can go in.

I don't think you need to touch config/abi/pre/gnu.ver, al lthe new
functions are inline and not exported from the library, so you don't
need to version symbols that aren't in the library (in fact, you
*can't* version symbols that aren't in the library!)

It looks like the changes to existing headers could be simplified by
not closing and reopening the namespace, i.e. instead of:

 namespace std _GLIBCXX_VISIBILITY(default)
 {
 _GLIBCXX_BEGIN_NAMESPACE_VERSION
[...]
 _GLIBCXX_END_NAMESPACE_VERSION
 } // namespace

+#if __cplusplus  201103L
+
+namespace std _GLIBCXX_VISIBILITY(default)
+{
+_GLIBCXX_BEGIN_NAMESPACE_VERSION
+
+inline namespace literals {
+inline namespace string_literals {
[...]

Just put the new stuff inside the already open namespace, i.e.

 namespace std _GLIBCXX_VISIBILITY(default)
 {
 _GLIBCXX_BEGIN_NAMESPACE_VERSION
 [...]
+#if __cplusplus  201103L
+
+inline namespace literals {
+inline namespace string_literals {
[...]
+} }
+#endif
+
 _GLIBCXX_END_NAMESPACE_VERSION
 } // namespace