简体   繁体   English

C ++ Compile-Time字符串操作

[英]C++ Compile-Time string manipulation

I looked at boost's mpl::string, but there doesn't seem to be an easy way of converting string literals to the single-quotation-integer-based format of mpl::string. 我查看了boost的mpl :: string,但似乎没有一种简单的方法可以将字符串文字转换为基于单引号整数的mpl :: string格式。 What I am trying to do is to generate at compile time an XML realization of some simple data structures using compile time strings. 我想要做的是在编译时使用编译时字符串生成一些简单数据结构的XML实现。 I am striving for having macros generate the structures themselves and insert a constant "meta" field inside them, containing said XML string. 我正在努力让宏自己生成结构并在其中插入一个包含所述XML字符串的常量“meta”字段。

The short answer is no, there is no easy way. 简短的回答是否定的,没有简单的方法。 At least not using C++ alone, and at compile time. 至少在编译时不单独使用C ++。 You can use scripts or some other code generator to produce mpl::string s with the correct literals. 您可以使用脚本或其他代码生成器生成具有正确文字的mpl::string C++0x will bring user defined literals [1], that allow an easy manipulation of literals, character by character, for example, using variadic templates. C ++ 0x将带来用户定义的文字[1],允许逐个字符地轻松操作文字,例如,使用可变参数模板。

  1. http://en.wikipedia.org/wiki/C%2B%2B0x#User-defined_literals http://en.wikipedia.org/wiki/C%2B%2B0x#User-defined_literals

Here is an article regarding the subject: http://akrzemi1.wordpress.com/2011/05/11/parsing-strings-at-compile-time-part-i/ . 这是一篇关于这个主题的文章: http//akrzemi1.wordpress.com/2011/05/11/parsing-strings-at-compile-time-part-i/ The author implements a simple RPN arithmetic calculator that works during compile-time using user string literals and constexpr. 作者使用用户字符串文字和constexpr实现了一个简单的RPN算术计算器,它在编译时工作。 I won't attempt to provide any more summary of the article here. 我不会尝试在此提供更多文章摘要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM