2

我想从字符串文字创建一个模板类: create_eq("abcdefg") get IEqual<'a','b','c','d','e'> 但是 ISO C++11 的用户定义文字仅支持整数文字和浮点文字:

template<char... Chars>
  IEqual<Chars...>  operator "" _suffix()
  {

    return {};
  }
    auto a=3423134_suffix;//yes
    auto b="abcdef"_suffix;//error!

我试试

using Char=int;
template<Char...ARGS>
class IEqual
{
public:

};

template<unsigned... I> struct Index{};
template<unsigned N, unsigned... I>
struct GenIndex : GenIndex<N-1, N-1, I...>{};
template<unsigned... I>
struct GenIndex<0, I...> : Index<I...>{};
template<unsigned I>
constexpr Char pos(const char*a)
{
    return a[I];
}
 template<unsigned...I>
   constexpr auto eq(Index<I...> ,const char*a)
       ->decltype(IEqual<(pos<I>(a))...>())// this error 
    {
        return {};
    }
    template<unsigned N>
   constexpr auto eq(const char(&a)[N])
   ->decltype(eq(GenIndex<N>{},a))
   {
       return eq(GenIndex<N>{},a);
   }

那我该怎么做呢?

4

0 回答 0