string2type

在泛型编程中,很多时候用到类型映射,类型转换等,比如int2type,type2type.

在有些时候可能需要将常量静态字符串作为类型,也就是string2type.

//https://github.com/ColinH/PEGTL 这个库将泛型大量用到了字符串处理中,其中就有string2type.

1.定义

 1 namespace blog
 2 {
 3     namespace internal{
 4         template< std::size_t N, std::size_t M >
 5         constexpr char string_at(const char(&c)[M]) noexcept
 6         {
 7             static_assert(M <= 101, "String longer than 100 (excluding terminating \\0)!");
 8             return (N < M) ? c[N] : 0;
 9         }
10 
11         template< char ... Cs > struct string;
12 
13         template<> struct string<> {};
14 
15         template< char ... Cs >
16         struct string
17         {
18             static constexpr char const value[sizeof...(Cs)+1] = { Cs...,'\0' };
19         };
20         template<char... C>
21         constexpr char const string<C...>::value[sizeof...(C)+1];
22     }
23     template< char ... Cs > struct string : internal::string< Cs ... > {};
24 
25     namespace internal
26     {
27         template< typename, char ... >
28         struct string_builder;
29         template< typename T >
30         struct string_builder< T > { using type = T; };
31         template< template< char ... > class S, char ... Hs, char C, char ... Cs >
32         struct string_builder< S< Hs ... >, C, Cs ... >
33             : std::conditional< C == '\0',
34             string_builder< S< Hs ... > >,
35             string_builder< S< Hs ..., C >, Cs ... > >::type
36         { };
37     }
38 }

2.实现

 1 #define MTL_INTERNAL_STRING_10(n,x)           \
 2    blog::internal::string_at< n##0 >( x ),     \
 3    blog::internal::string_at< n##1 >( x ),     \
 4    blog::internal::string_at< n##2 >( x ),     \
 5    blog::internal::string_at< n##3 >( x ),     \
 6    blog::internal::string_at< n##4 >( x ),     \
 7    blog::internal::string_at< n##5 >( x ),     \
 8    blog::internal::string_at< n##6 >( x ),     \
 9    blog::internal::string_at< n##7 >( x ),     \
10    blog::internal::string_at< n##8 >( x ),     \
11    blog::internal::string_at< n##9 >( x )
12 #define blog_str2type(x) \
13    blog::internal::string_builder< blog::internal::string<>, \
14                                        MTL_INTERNAL_STRING_10(,x),                \
15                                        MTL_INTERNAL_STRING_10(1,x),               \
16                                        MTL_INTERNAL_STRING_10(2,x),               \
17                                        MTL_INTERNAL_STRING_10(3,x),               \
18                                        MTL_INTERNAL_STRING_10(4,x),               \
19                                        MTL_INTERNAL_STRING_10(5,x),               \
20                                        MTL_INTERNAL_STRING_10(6,x),               \
21                                        MTL_INTERNAL_STRING_10(7,x),               \
22                                        MTL_INTERNAL_STRING_10(8,x),               \
23                                        MTL_INTERNAL_STRING_10(9,x) >::type

3.使用

1 typedef blog_str2type("abcdefg") abcdefg;
2 std::cout << abcdefg::value << std::endl;

4.结果

      abcdefg

5.优缺点

 

转载于:https://www.cnblogs.com/goooon/p/5643667.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值