我刚刚安装了gcc-4.8.1,当我意识到我可以执行-std = C++ 1y并获得多行constexpr 。我很想知道,有没有做这项工作?从C++中的用户定义文字返回std :: array 11
#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做的事情。无论如何,从参数中获得一个常量表达式?
这看起来很酷,我会在它去这个afternoon.C++ 14将支持多constexpr功能,但它看起来像我错了,这并没有使它成为GCC呢。 – HNJSlater
这对我很有用,非常感谢! – HNJSlater