我刚刚安装了gcc-4.8.1并且发现可以使用-std=c++1y来获取多行的constexpr,我感到非常兴奋。但是我很好奇是否有任何方法使它能够工作呢?
#include <array>
constexpr auto operator "" _a1 (const char* text, const size_t size) -> std::array<char,size> {
std::array<char,size>() blah;
std::strncpy(blah.data(), test, size);
// do some stuff to blah at compile time
return blah;
}
int main() {
auto blah = "hello world"_a2;
}
但我遇到了一个非常糟糕的问题:
$ g++ test.cpp -std=gnu++1y -Wall -Werror -Wextra -Weffc++ -pedantic
test.cpp:3:100: error: use of parameter ‘size’ outside function body
constexpr auto operator "" _a1 (const char* text, const size_t size) -> std::array<char,size> {
^
test.cpp:3:100: error: use of parameter ‘size’ outside function body
test.cpp:3:100: error: use of parameter ‘size’ outside function body
test.cpp:3:104: error: template argument 2 is invalid
constexpr auto operator "" _a1 (const char* text, const size_t size) -> std::array<char,size> {
^
test.cpp: In function ‘int main()’:
test.cpp:26:17: error: unable to find string literal operator ‘operator"" _a1’
auto blah = "hello world"_a1;
有没有办法实现这个功能?我无法从constexpr返回std :: string,并且似乎没有任何模板或decltype可以使用。有没有办法从参数中获取常量表达式?