std::vector 个函数指针:不同的模板参数
std::vector of function pointers: different template parameters
下面为什么编译
std::vector<int(*)(double)> func_ptrs;
但这不
std::vector<int(double)> func_ptrs
?
在第二种情况下,我收到了那些难看的 STL 错误消息之一,所以我不打算将所有内容都放在这里,但在消息的末尾,我收到了这个
/usr/include/c++/4.8/bits/stl_construct.h:102:30: error: ISO C++ forbids incrementing a pointer of type ‘int (*)(double)’ [-fpermissive]
for (; __first != __last; ++__first)
这似乎暗示 C++ 将类型 int(double)
转换为 int (*) (double)
。我的印象是 int(*)(double)
和 int(double)
是等价的吗?还是我错了?
想澄清一下。提前致谢。
int(double)
其实是函数类型,不是函数指针。在许多情况下它会退化为函数指针,但这里不会。例如,您不能将 sizeof
与函数类型一起使用 - 这对于 vector
的分配器至关重要。
至于你的具体错误:add_pointer_t<int(double)>
(或多或少被vector
的迭代器内部或直接使用)是int(*)(double)
并且不能递增,因为它执行这样的操作没有意义。
下面为什么编译
std::vector<int(*)(double)> func_ptrs;
但这不
std::vector<int(double)> func_ptrs
?
在第二种情况下,我收到了那些难看的 STL 错误消息之一,所以我不打算将所有内容都放在这里,但在消息的末尾,我收到了这个
/usr/include/c++/4.8/bits/stl_construct.h:102:30: error: ISO C++ forbids incrementing a pointer of type ‘int (*)(double)’ [-fpermissive]
for (; __first != __last; ++__first)
这似乎暗示 C++ 将类型 int(double)
转换为 int (*) (double)
。我的印象是 int(*)(double)
和 int(double)
是等价的吗?还是我错了?
想澄清一下。提前致谢。
int(double)
其实是函数类型,不是函数指针。在许多情况下它会退化为函数指针,但这里不会。例如,您不能将 sizeof
与函数类型一起使用 - 这对于 vector
的分配器至关重要。
至于你的具体错误:add_pointer_t<int(double)>
(或多或少被vector
的迭代器内部或直接使用)是int(*)(double)
并且不能递增,因为它执行这样的操作没有意义。