$include_dir="/home/hyper-archives/boost/include"; include("$include_dir/msg-header.inc") ?>
Subject: Re: [boost] compile time parser generator
From: Ábel Sinkovics (abel_at_[hidden])
Date: 2012-01-09 01:31:36
Hi Dave,
> constexpr is somewhat crippled: although a constexpr function executes
> at compile-time, it has to obey the same rules as any other function.
> For example, its return type can depend on the type, but never on the
> *value*, of its arguments, and once you're inside the function, the
> contents of the argument are not treated as compile-time constants.
> So, I think constexpr actually doesn't promise it.
A constexpr can still be used to access the characters of a string at
compile-time. Having access to each character we can build an MPL list
of characters. Here is an example of what I'm thinking of:
---- using namespace boost::mpl; template <int N> constexpr char nth(const char s[N], int n) { return n >= N ? 0 : s[n]; } #define S "cool" typedef push_back< push_back< push_back< push_back< push_back< push_back< string<>, char_<nth<sizeof(S)>(S, 0)> >::type, char_<nth<sizeof(S)>(S, 1)> >::type, char_<nth<sizeof(S)>(S, 2)> >::type, char_<nth<sizeof(S)>(S, 3)> >::type, char_<nth<sizeof(S)>(S, 4)> >::type, char_<nth<sizeof(S)>(S, 5)> >::type str; int main() { std::cout << c_str<str>::type::value << std::endl; } ---- The code getting the characters one by one and building the MPL list (or string in the above example) can be generated by a macros - I know it is not that nice and the length of the string will be tricky, but we'll have something. The above code snippet compiles with gcc 4.6. Regards, Ábel