使用 std::array::size 实例化 std::array 时出错
Error when instantiating std::array using std::array::size
示例代码测试.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;
}
使用 g++ 4.8.4 (Ubuntu 14.04( 编译
g++ -Wall -std=c++0x test.cpp -o test
给出以下错误消息
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;
^
有人可以解释这个错误吗?为什么第一个示例有效,而第二个示例无法编译?
类型std::string不是文字类型,这意味着在编译时不能将其作为constexpr函数的一部分进行操作。在编译时,编译器尝试计算array_string的 size(( 函数。您在第一个错误中看到的函数第一个类型参数设置为 std::basic_string
我会推荐您以下内容以了解有关constexpr的更多信息。
http://en.cppreference.com/w/cpp/language/constexpr
我会推荐您以下内容来了解文字类型。
http://en.cppreference.com/w/cpp/concept/LiteralType
最后,尝试以下简单的代码,您将看到 int 和 float 是文字类型,而 std::string 不是。您可以尝试使用其他类型,以查看什么是或不是文本类型。
#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;
}
希望有帮助。
John
相关文章:
- C++11 中不同类型的对象的 std::array 的替代方案
- constexpr begin of a std::array
- C++如果必须在编译时确定大小,std::array 有什么意义?
- 标准::unordered_map 中的 std::array 的值初始化
- 确保编译时的特定 std::array 位置
- std::array的长度有大小限制吗?
- 将 std::array 移动到另一个 std::array
- 为什么 std::shared_ptr 被认为是"heavy"和"expensive",但 std::array "same perfprmance as plain (c-style) arrays
- std::bind on statd::array 的运算符 []
- 检查输入 std::array 指针数据是否等于某个常量数组
- 如何读/写或遍历 std::array 中的特定元素范围?
- 是否可以使用 std::array 作为 POD 结构的数据容器?
- 如何在C++中传递一个大小未知的 std::array?
- 在现代C++中将 std::array<std:::array<T,N>> 转换为 std::vector<T>
- 在 C++ 中访问 std::array<std::array> 元素
- 将标量+std::array+std::tuple转换为一个大元组
- 如何初始化 std::array<std::array<T, 2>, 2> 的对象?
- 初始化 std::p air<double, std::array<std::p air<double, double>, 3> >
- 单独移动数组(raw array, std::array, std::vector)的每个元素
- 包装方法返回 c++ std::array<std::string, 4> in cython