add _indexes User-defined literals

This commit is contained in:
bolero-MURAKAMI 2013-04-07 21:55:55 +09:00
parent 410c369a6a
commit f0dfb208e0
3 changed files with 82 additions and 0 deletions

View file

@ -0,0 +1,67 @@
#ifndef SPROUT_INDEX_TUPLE_UDL_HPP
#define SPROUT_INDEX_TUPLE_UDL_HPP
#include <sprout/config.hpp>
#include <sprout/index_tuple/index_tuple.hpp>
#if SPROUT_USE_USER_DEFINED_LITERALS
#include <type_traits>
#include <sprout/index_tuple/make_index_tuple.hpp>
namespace sprout {
namespace detail {
template<typename IntType, char... Chars>
struct digits_to_int;
template<typename IntType, char C>
struct digits_to_int<IntType, C>
: public std::integral_constant<IntType, IntType(C - 48)>
{};
template<typename IntType, char Head, char... Tail>
struct digits_to_int<IntType, Head, Tail...>
: public std::integral_constant<IntType, 10 * IntType(Head - 48) + sprout::detail::digits_to_int<IntType, Tail...>::value>
{};
} // namespace detail
//
// indexes_result
//
template<char... Chars>
struct indexes_result
: public sprout::make_index_tuple<sprout::detail::digits_to_int<sprout::index_t, Chars...>::value>
{};
//
// uindexes_result
//
template<char... Chars>
struct uindexes_result
: public sprout::make_uindex_tuple<sprout::detail::digits_to_int<sprout::uindex_t, Chars...>::value>
{};
namespace udl {
//
// _indexes
//
template<char... Chars>
SPROUT_CONSTEXPR typename sprout::indexes_result<Chars...>::type
operator "" _indexes() {
return sprout::indexes_result<Chars...>::make();
}
//
// _uindexes
//
template<char... Chars>
SPROUT_CONSTEXPR typename sprout::uindexes_result<Chars...>::type
operator "" _uindexes() {
return sprout::uindexes_result<Chars...>::make();
}
} // namespace udl
using sprout::udl::operator "" _indexes;
using sprout::udl::operator "" _uindexes;
} // namespace sprout
#endif // #if SPROUT_USE_USER_DEFINED_LITERALS
#endif // #ifndef SPROUT_INDEX_TUPLE_UDL_HPP

View file

@ -4,5 +4,6 @@
#include <sprout/config.hpp>
#include <sprout/index_tuple/make_indexes.hpp>
#include <sprout/index_tuple/enable_make_indexes.hpp>
#include <sprout/index_tuple/udl.hpp>
#endif // #ifndef SPROUT_INDEX_TUPLE_UTILITY_HPP

View file

@ -6,6 +6,7 @@
#include <sprout/config.hpp>
#include <sprout/type/tuple.hpp>
#include <sprout/type/integral_array.hpp>
#include <sprout/tuple/tuple/get.hpp>
namespace sprout {
namespace types {
@ -47,4 +48,17 @@ namespace std {
#endif
} // namespace std
namespace sprout {
//
// tuple_get
//
template<std::size_t I, typename T, T... Values>
inline SPROUT_CONSTEXPR typename std::tuple_element<I, sprout::types::basic_string<T, Values...> >::type
tuple_get(sprout::types::basic_string<T, Values...>) SPROUT_NOEXCEPT {
static_assert(I < sizeof...(Values), "tuple_get: index out of range");
typedef typename std::tuple_element<I, sprout::types::basic_string<T, Values...> >::type type;
return type();
}
} // namespace sprout
#endif // #ifndef SPROUT_TYPE_STRING_STRING_HPP