Constexpr with string bypass?

This previously answered question explains why the code I posted below does not work. I have the following question: is there a workaround that is conceptually equivalent, i.e. Is compilation string concatenation achieved, but implemented in a way that C ++ 11 actually supports? Using std :: string is completely immaterial.

constexpr std::string foo() { return std::string("foo"); } constexpr std::string bar() { return std::string("bar"); } constexpr std::string foobar() { return foo() + bar(); } 
+4
c ++ stdstring c ++ 11 constexpr
source share
2 answers

Compilation "string" compilation:

 #include <iostream> #include <string> template <char ... CTail> struct MetaString { static std::string string() { return std::string{CTail...}; } }; template <class L, class R> struct Concatenate; template <char ... LC, char ... RC> struct Concatenate<MetaString<LC ... >, MetaString<RC ... >> { typedef MetaString<LC ..., RC ... > Result; }; int main() { typedef MetaString<'f', 'o', 'o'> Foo; typedef MetaString<'b', 'a', 'r'> Bar; typedef typename Concatenate<Foo, Bar>::Result FooBar; std::cout << Foo::string() << std::endl; // foo std::cout << Bar::string() << std::endl; // bar std::cout << FooBar::string() << std::endl; // foobar } 
+7
source share

Sprout C ++ libraries provide the constexpr string. see: https://github.com/bolero-MURAKAMI/Sprout/blob/master/libs/string/test/string.cpp

+3
source share

All Articles