コンパイル時に Sprout.String を出力する
先日行ったやり方で、コンパイル時に Sprout.String の内容を出力してみました。
[ソース]
#include <boost/preprocessor/repetition/enum.hpp> #include <sprout/string.hpp> template<std::size_t Index, typename String> constexpr char at_c(String const& str){ return Index >= str.length() ? char('\0') : str[Index]; } template<char... cs> struct chars{}; #define STRING_MAX_LENGTH 16 #define DECL_AT_C(z, n, text) \ at_c<n>(text) #define _(str) \ chars<BOOST_PP_ENUM(STRING_MAX_LENGTH, DECL_AT_C, str)> #include <iostream> #include <boost/mpl/print.hpp> #include <boost/mpl/equal.hpp> template<typename String> void print(){ namespace m =boost::mpl; typedef typename m::print<String>::type output; } int main(){ constexpr auto mado = sprout::to_string("mado"); constexpr auto homu = sprout::to_string("homu"); print<_(mado + homu)>(); return 0; }
[コンパイル出力]
In file included from test/main.cpp|26| 0: || D:/boost/boost_1_49_0/boost/mpl/print.hpp: In instantiation of 'struct boost::mpl::print<chars<'m', 'a', 'd', 'o', 'h', 'o', 'm', 'u', '\000', '\000', '\000', '\000', '\000', '\000', '\000', '\000'> >': main.cpp|33 col 42| required from 'void print() [with String = chars<'m', 'a', 'd', 'o', 'h', 'o', 'm', 'u', '\000', '\000', '\000', '\000', '\000', '\000', '\000', '\000'>]' main8.cpp|40 col 24| required from here \home\work\software\lib\cpp\boost\boost_1_49_0\boost\mpl\print.hpp|55 col 10| warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
見やすいとはいえませんが、コンパイル時にちゃんと文字列が分かるように出力されていますね。
フォーマットを指定して、整数なんかも出力できるようになればかなり便利そうですね。
これはまたメタプログラミングが捗りそう。