[英]Error when instantiating std::array using std::array::size
Example code test.cpp 示例代码test.cpp
#include <array>
#include <string>
int main ()
{
// OK
const std::array<int, 2> array_int = {42, 1337};
std::array<float, array_int.size()> array_float_ok;
// Error
const std::array<std::string, 2> array_string = {"foo", "bar"};
std::array<float, array_string.size()> array_float_error;
return 0;
}
Compiling with g++ 4.8.4 (Ubuntu 14.04) 使用g ++ 4.8.4(Ubuntu 14.04)进行编译
g++ -Wall -std=c++0x test.cpp -o test
Gives the following error message 给出以下错误信息
test.cpp: In function ‘int main()’:
test.cpp:14:39: error: call to non-constexpr function ‘constexpr std::array<_Tp, _Nm>::size_type std::array<_Tp, _Nm>::size() const [with _Tp = std::basic_string<char>; long unsigned int _Nm = 2ul; std::array<_Tp, _Nm>::size_type = long unsigned int]’
std::array<float, array_string.size()> array_float_error;
^
In file included from test.cpp:1:0:
/usr/include/c++/4.8/array:162:7: note: ‘constexpr std::array<_Tp, _Nm>::size_type std::array<_Tp, _Nm>::size() const [with _Tp = std::basic_string<char>; long unsigned int _Nm = 2ul; std::array<_Tp, _Nm>::size_type = long unsigned int]’ is not usable as a constexpr function because:
size() const noexcept { return _Nm; }
^
/usr/include/c++/4.8/array:162:7: error: enclosing class of constexpr non-static member function ‘constexpr std::array<_Tp, _Nm>::size_type std::array<_Tp, _Nm>::size() const [with _Tp = std::basic_string<char>; long unsigned int _Nm = 2ul; std::array<_Tp, _Nm>::size_type = long unsigned int]’ is not a literal type
/usr/include/c++/4.8/array:81:12: note: ‘std::array<std::basic_string<char>, 2ul>’ is not literal because:
struct array
^
/usr/include/c++/4.8/array:81:12: note: ‘std::array<std::basic_string<char>, 2ul>’ has a non-trivial destructor
test.cpp:14:39: error: call to non-constexpr function ‘constexpr std::array<_Tp, _Nm>::size_type std::array<_Tp, _Nm>::size() const [with _Tp = std::basic_string<char>; long unsigned int _Nm = 2ul; std::array<_Tp, _Nm>::size_type = long unsigned int]’
std::array<float, array_string.size()> array_float_error;
^
test.cpp:14:40: note: in template argument for type ‘long unsigned int’
std::array<float, array_string.size()> array_float_error;
^
test.cpp:14:59: error: invalid type in declaration before ‘;’ token
std::array<float, array_string.size()> array_float_error;
^
test.cpp:9:39: warning: unused variable ‘array_float_ok’ [-Wunused-variable]
std::array<float, array_int.size()> array_float_ok;
^
test.cpp:14:42: warning: unused variable ‘array_float_error’ [-Wunused-variable]
std::array<float, array_string.size()> array_float_error;
^
Can someone explain this error? 有人可以解释这个错误吗? Why does the first example work while the second one does not compile?
为什么第一个示例可以工作而第二个示例不能编译?
The type std::string is not a literal type meaning that it cannot be manipulated as part of a constexpr function at compile time. std :: string类型不是文字类型,这意味着在编译时不能将其作为constexpr函数的一部分进行操作。 At compile time, the compiler attempts to evaluate the array_string's size() function.
在编译时,编译器尝试评估array_string的size()函数。 The functions first type parameter as you can see in the first error is set to std::basic_string < char > (aka std::string);
如您在第一个错误中看到的那样,函数的第一类型参数设置为std :: basic_string <char>(aka std :: string); therefore, since std::string is not a literal type, the function cannot be evaluated as a constexpr function at compile time and you have an error.
因此,由于std :: string不是文字类型,因此在编译时无法将该函数评估为constexpr函数,并且会出现错误。
I would refer you to the following to learn more about constexpr. 我希望您参考以下内容,以了解有关constexpr的更多信息。
http://en.cppreference.com/w/cpp/language/constexpr http://en.cppreference.com/w/cpp/language/constexpr
I would refer you to the following to learn about literal types. 我希望您参考以下内容,以了解文字类型。
http://en.cppreference.com/w/cpp/concept/LiteralType http://en.cppreference.com/w/cpp/concept/LiteralType
Finally, try the following simple code and you will see that int and float are literal types and std::string is not. 最后,尝试以下简单代码,您会发现int和float是文字类型,而std :: string不是。 You can try it out with other types to see what are or aren't literal types.
您可以尝试使用其他类型来查看什么是文字类型,或者不是文字类型。
#include <iostream>
int main(int argc, char** argv)
{
std::cout << std::is_literal_type<int>::value << std::endl;
std::cout << std::is_literal_type<float>::value << std::endl;
std::cout << std::is_literal_type<std::string>::value << std::endl;
return 0;
}
Hope that helps. 希望能有所帮助。
John 约翰
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.