• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1//  (C) Copyright John Maddock 2013
2
3//  Use, modification and distribution are subject to the
4//  Boost Software License, Version 1.0. (See accompanying file
5//  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6
7//  See http://www.boost.org/libs/config for more information.
8
9//  MACRO:         BOOST_NO_CXX11_USER_DEFINED_LITERALS
10//  TITLE:         C++11 user defined literals.
11//  DESCRIPTION:   The compiler does not support the C++11 literals including user-defined suffixes.
12
13#include <memory>
14
15namespace boost_no_cxx11_user_defined_literals {
16
17struct my_literal
18{
19   my_literal() : val(0) {}
20   my_literal(int i) : val(i) {}
21   my_literal(const my_literal& a) : val(a.val) {}
22   bool operator==(const my_literal& a) const { return val == a.val; }
23   int val;
24};
25
26template <unsigned base, unsigned long long val, char... Digits>
27struct parse_int
28{
29    // The default specialization is also the termination condition:
30    // it gets invoked only when sizeof...Digits == 0.
31    static_assert(base<=16u,"only support up to hexadecimal");
32    static constexpr unsigned long long value{ val };
33};
34
35template <unsigned base, unsigned long long val, char c, char... Digits>
36struct parse_int<base, val, c, Digits...>
37{
38    static constexpr unsigned long long char_value = (c >= '0' && c <= '9')
39            ? c - '0'
40            : (c >= 'a' && c <= 'f')
41            ? c - 'a'
42            : (c >= 'A' && c <= 'F')
43            ? c - 'A'
44            : 400u;
45    static_assert(char_value < base, "Encountered a digit out of range");
46    static constexpr unsigned long long value{ parse_int<base, val * base +
47char_value, Digits...>::value };
48};
49
50my_literal operator "" _suf1(unsigned long long v)
51{
52   return my_literal(v);
53}
54template <char...PACK>
55my_literal operator "" _bin()
56{
57   return parse_int<2, 0, PACK...>::value;
58}
59
60int test()
61{
62   my_literal a = 0x23_suf1;
63   my_literal b = 1001_bin;
64   return ((a == my_literal(0x23)) && (b == my_literal(9))) ? 0 : 1;
65}
66
67}
68