compile-time string literal manipulation?

I'm a new to metaprogramming so please bare with the stupid questions: I have a requirement to convert string literals to a specified format at compile-time. In otherwords, given a literal like "3A689", I need to verify the value and, if not valid, generate a correct value such that: //pseudocode const char ch[] = verify<"3A689">::valid_value; would result in something like: const char ch[] = "03A689X"; Can I use Boost to write a template to do this? I know that is extremely vague, but does anyone have any thoughts? Thanks.

On 10/27/2010 2:46 PM, Stiles Watson wrote:
I'm a new to metaprogramming so please bare with the stupid questions:
I have a requirement to convert string literals to a specified format at compile-time. In otherwords, given a literal like "3A689", I need to verify the value and, if not valid, generate a correct value such that:
//pseudocode const char ch[] = verify<"3A689">::valid_value;
would result in something like:
const char ch[] = "03A689X";
Can I use Boost to write a template to do this?
I know that is extremely vague, but does anyone have any thoughts?
Use mpl::string: http://www.boost.org/doc/libs/1_44_0/libs/mpl/doc/refmanual/string.html You might find the interface less than ideal. C++ doesn't allow strings as template parameters. -- Eric Niebler BoostPro Computing http://www.boostpro.com

On Thu, Oct 28, 2010 at 1:41 AM, Eric Niebler
You might find the interface less than ideal. C++ doesn't allow strings as template parameters.
-- Eric Niebler BoostPro Computing http://www.boostpro.com
Hi Eric,
just a question: It is possible to pass array objects with external linkage
as template parameters, is there any way one can manipulate them at compile
time? I am wondering, if one can pass begin and end iterator as template
parameters and verify such a string. That might have real impact on compile
time...
If I can use such a function:
tempalte

2010/10/28 Ovanes Markarian
On Thu, Oct 28, 2010 at 1:41 AM, Eric Niebler
wrote: You might find the interface less than ideal. C++ doesn't allow strings as template parameters.
-- Eric Niebler BoostPro Computing http://www.boostpro.com
Hi Eric,
just a question: It is possible to pass array objects with external linkage as template parameters, is there any way one can manipulate them at compile time? I am wondering, if one can pass begin and end iterator as template parameters and verify such a string. That might have real impact on compile time...
If I can use such a function: tempalte
void array_len(char (&arr)[]) {} I might be able to pass instantiated function template function to the template to calculate the string length and than I have begin and length of the string, and can apply recusion at compile time. What do you think about it?
You can access the pointer at compile time but not the characters. So you indeed can have 'begin' and 'length', but accessing the content at compile time is still impossible. Roman Perepelitsa.

Eric Niebler wrote:
On 10/27/2010 2:46 PM, Stiles Watson wrote:
I'm a new to metaprogramming so please bare with the stupid questions:
I have a requirement to convert string literals to a specified format at compile-time. In otherwords, given a literal like "3A689", I need to verify the value and, if not valid, generate a correct value such that:
//pseudocode const char ch[] = verify<"3A689">::valid_value;
would result in something like:
const char ch[] = "03A689X";
Can I use Boost to write a template to do this?
I know that is extremely vague, but does anyone have any thoughts?
Use mpl::string:
http://www.boost.org/doc/libs/1_44_0/libs/mpl/doc/refmanual/string.html
You might find the interface less than ideal. C++ doesn't allow strings as template parameters.
Thanks Eric. Question. For the example given at the link above, it has typedef mpl::string<'hell','o wo','rld'> hello; Why would it not be typedef mpl::string<'h','e','l','l','o',' ','w','o','r','l','d'> hello; OR if the example is correct, why could you not simply do typedef mpl::string<'hello world'> hello; Like I said, I'm new to this so I'm sure there is something fundamental that I'm missing. Stiles

2010/10/28, Stiles Watson
Question. For the example given at the link above, it has
typedef mpl::string<'hell','o wo','rld'> hello;
Why would it not be
typedef mpl::string<'h','e','l','l','o',' ','w','o','r','l','d'> hello;
OR if the example is correct, why could you not simply do
typedef mpl::string<'hello world'> hello;
Note that they are multi-character character constants (in single quote), not strings (in quotes), and multi-character character may have at most the size of int, so you cannot have a single 'hello world'.

Question. For the example given at the link above, it has
typedef mpl::string<'hell','o wo','rld'> hello;
That's weird.
Why would it not be
typedef mpl::string<'h','e','l','l','o', ' ','w','o','r','l','d'> hello;
I would think this was correct, as well.
OR if the example is correct, why could you not simply do
typedef mpl::string<'hello world'> hello;
Non-type template arguments can only be constant integers or (maybe in this case) multi-byte characters. A four-byte character value might be the standard limit here. (To be honest, I haven't really worked with Unicode or anything else beyond one-byte characters. Someone else may have a better answer for you.) Cromwell D. Enage

On Fri, Oct 29, 2010 at 11:04 AM, Cromwell Enage
Why would it not be typedef mpl::string<'h','e','l','l','o',' ','w','o','r','l','d'> hello; I would think this was correct, as well.
It probably is, but uses more template parameters, probably leading to longer compile time and bigger pre-processed code (just guesses, I'm no expert). It's also longer to type :)
OR if the example is correct, why could you not simply do typedef mpl::string<'hello world'> hello;
Because that's invalid.
Non-type template arguments can only be constant integers or (maybe in this case) multi-byte characters. A four-byte character value might be the standard limit here. [...]
As [1] points out, using 'abcd' is a "fairly unknown—and arguably somewhat implementation-defined—existence of multi-character literals" feature of C++. [2] further points to STD 6.4.4.4p10 stating it is indeed implementation-defined. But Eric Niebler (templatr-extraordinaire), made it work for known compilers. So yes it's a bit weird at first, but it just works! --DD [1] http://arcticinteractive.com/2009/04/18/compile-time-string-hashing-boost-mp... [2] http://bytes.com/topic/c/answers/739845-multi-character-constant
participants (7)
-
Cromwell Enage
-
Dominique Devienne
-
Eric Niebler
-
Ovanes Markarian
-
Roman Perepelitsa
-
Stiles Watson
-
TONGARI