2

I want to create a template class from string literal: create_eq("abcdefg") get IEqual<'a','b','c','d','e'> but ISO C++11's User-defined literals only support integer-literal and floating-literal :

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

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

I try

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);
   }

so how can I do it?

fe263
  • 215
  • 1
  • 7
  • Hating to be the bringer of sad news, but.... [read this.](http://stackoverflow.com/questions/5547852/string-literals-not-allowed-as-non-type-template-parameters) But I think you already knew that. Are you looking for work-arounds? – WhozCraig Aug 28 '13 at 07:18
  • @WhozCraig Well, I'll be the [bearer of good news](http://stackoverflow.com/questions/4583022/c-can-a-macro-expand-abc-into-a-b-c/). – Rapptz Aug 28 '13 at 07:28
  • 1
    Also, for the record you can have user defined literals for `const char[N]`. – Rapptz Aug 28 '13 at 07:31
  • I want to write a Compile-time parser.now I have to write a run-time parser – fe263 Aug 28 '13 at 07:36
  • @Rapptz the latter briefly eluded to at the end of the first answer in the first link, though I like your decl more (honestly makes more sense at least in my head). – WhozCraig Aug 28 '13 at 07:36
  • "but ISO C++11's User-defined literals only support integer-literal and floating-literal" this is wrong. Check out this http://ideone.com/cEBUmX – user1233963 Aug 28 '13 at 07:53
  • @user1233963 my mean is "template" Form. – fe263 Aug 30 '13 at 06:59

0 Answers0