|
Prev: Does taking address of function template specialization not force instantiation?
Next: Writing a std::allocator for a map<>
From: Piotr Rak on 12 Apr 2008 03:09 Hi all, I was thinking about language feature allowing convert string literal into variadic character pack (char...). Here is real life use case, which comes from Yard parser library (http://yard-parser.sourceforge.net/cgi-bin/index.cgi). It shall define parser that matches sequence of characters. Exact implementation doesn't matter. It has been slightly modified to use variadic templates template <char Char1_, char Char2_, char...MoreChars_> class CharSeq { static const unsigned int length = sizeof...(MoreChars_) + 2; inline static char get_nth(unsigned int n); public: template <typename ParserState_> static bool match(ParserState_& state); }; typedef CharSeq<'c', 'a', 't', 'c', 'h'> CatchKeyword; // which could be: //typedef CharSeq<"catch"> CatchKeyword; There is proposal of 'User-defined literals' (http://www.open-std.org/ jtc1/sc22/wg21/docs/papers/2007/n2378.pdf), but atleast in my understanding, it fails to provide way of doing that. Is my assumption is correct? If yes, what are technical reasons from preventing that? Cheers, Piotr Rak -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Looney on 13 Apr 2008 03:45 On Apr 13, 4:09 am, Piotr Rak <piotr....(a)gmail.com> wrote: > Hi all, > > I was thinking about language feature allowing convert string literal > into variadic character pack (char...). > > Here is real life use case, which comes from Yard parser library > (http://yard-parser.sourceforge.net/cgi-bin/index.cgi). > It shall define parser that matches sequence of characters. > Exact implementation doesn't matter. > > It has been slightly modified to use variadic templates > > template <char Char1_, char Char2_, char...MoreChars_> > class CharSeq > { > static const unsigned int length = sizeof...(MoreChars_) + 2; > > inline static char get_nth(unsigned int n); > > public: > template <typename ParserState_> > static bool match(ParserState_& state); > > }; > > typedef CharSeq<'c', 'a', 't', 'c', 'h'> CatchKeyword; > // which could be: > //typedef CharSeq<"catch"> CatchKeyword; > > There is proposal of 'User-defined literals' (http://www.open-std.org/ > jtc1/sc22/wg21/docs/papers/2007/n2378.pdf), but atleast in my > understanding, it fails to provide way of doing that. > > Is my assumption is correct? > If yes, what are technical reasons from preventing that? > > Cheers, Piotr Rak the current standard does not support Variadic Templates they may be added in the c++ox revision i do not think any c++ compilers other than gcc support this http://www.osl.iu.edu/~dgregor/cpp/variadic-templates.html so i fyou do wnat portable code or are using som other compiler you would need to declare your class templates with the required number of template parameters. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Mathias Gaunard on 13 Apr 2008 09:21 On 12 avr, 20:09, Piotr Rak <piotr....(a)gmail.com> wrote: > There is proposal of 'User-defined literals' (http://www.open-std.org/ > jtc1/sc22/wg21/docs/papers/2007/n2378.pdf), but atleast in my > understanding, it fails to provide way of doing that. User-defined literals receive a C-string. That's a fairly inefficient design, of course. > If yes, what are technical reasons from preventing that? None really. It just seems people working on the standard are not that interested in compile-time computation. Ideally, strings should be manipulable at compile-time as easily as at runtime. There are so many uses to this (especially for parser generators and the like). -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Brendan on 13 Apr 2008 09:21 > the current standard does not support Variadic Templates they may be > added in the c++ox revision > i do not think any c++ compilers other than gcc support thishttp://www.osl.iu.edu/~dgregor/cpp/variadic-templates.html > so i fyou do wnat portable code or are using som other compiler you > would need to declare > your class templates with the required number of template parameters. The op's question seems to be about the C++0x standard as it exists at this time, which supports things like variadic templates. He's asking whether there is some mechanism in the in progress C++0x standard that would allow him to take apart a string literals character by character at compile time using a template metaprogram. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Daniel Krügler on 13 Apr 2008 09:23
On 13 Apr., 20:45, Looney <hardy_melbou...(a)hotmail.com> wrote: > the current standard does not support Variadic Templates they may be > added in the c++ox revision > i do not think any c++ compilers other than gcc support > thishttp://www.osl.iu.edu/~dgregor/cpp/variadic-templates.html > so i fyou do wnat portable code or are using som other compiler you > would need to declare > your class templates with the required number of template parameters. IMO the OP is aware that *current* C++ does not have variadic templates. But realizing that C++0x will be finished somewhere in 2009 his example is quite reasonable, because variadic templates do have *very* high chances to be introduced into C++0x. I find his extension proposal to allow a string-literal as an alternative argument of variadic non-type templates (of character type) quite interesting. This is IMO the first approach, where I would see any chance of acceptance on implementors side. Early proposals - which had no variadic templates available and usually reasoned about 'char const*' template parameters - lead to several problematic situations, which I do not see to exist for this proposal. Just my personal 2 Euro cent, Daniel Kr�gler -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |