Konrad Kapp Konrad Kapp - 15 days ago 4x
C++ Question

std::vector of function pointers: different template parameters

Why does the following compile

std::vector<int(*)(double)> func_ptrs;

but this does not

std::vector<int(double)> func_ptrs


I get one of those ugly STL error messages in the second case, so I'm not going to put everything here, but at the end of the message I get this

/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)

This seems to imply that C++ cast the type
int (*) (double)
. I was under the impression that
are equivalent anyway? Or am I wrong?

Would like to have some clarification. Thanks in advance.


int(double) is actually a function type, not a function pointer. It decays to function pointer in many cases, but not here. You can't use sizeof with a function type, for example - and that's vital for vector's allocator.

As for your specific error: add_pointer_t<int(double)> (more or less this is used by vector's iterator, internally or directly) is int(*)(double) and cannot be incremented, because it makes no sense to perform such operation.