I have a class that can be constexpr constructed. I use it do alter some string literals at compile time. In this contrived example you can see that the constexpr
constructor is called which will then process a local member data_
. But how do I get the data_
out finally? My approach doesn't seem to work:
(Demo)
#include <cstdio>
#include <cstddef>
template <const char* S>
struct cxprstring
{
constexpr cxprstring() : data_{S} {
// ...
// (further processing of data_)
}
constexpr static const char value[] = cxprstring<S>{}.data_;
char data_[] = {};
};
template <const char* S>
constexpr const char format_view[] = cxprstring<S>::value;
constexpr const char a[] = "hello ";
int main()
{
printf("%s\n", format_view<a>);
}
It yields the following errors:
<source>:12:59: error: initializer fails to determine size of 'cxprstring<S>::value'
12 | constexpr static const char value[] = cxprstring<S>{}.data_;
| ~~~~~~~~~~~~~~~~^~~~~
<source>:18:53: error: initializer fails to determine size of 'format_view<S>'
18 | constexpr const char format_view[] = cxprstring<S>::value;
| ^~~~~
How can I make it work?