Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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) to int (*) (double). I was under the impression that int(*)(double) and int(double) are equivalent anyway? Or am I wrong?

Would like to have some clarification. Thanks in advance.

like image 337
Konrad Avatar asked Oct 08 '16 21:10

Konrad


1 Answers

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.

like image 133
krzaq Avatar answered Sep 17 '22 12:09

krzaq