0

I have a function prototype that's returning this error. I'm also getting a warning for extended list being for -std=c++11 or -std=gnu++11

float lb(float* data, int y);
float ub (float* data, int y);

float operation(Stats* ptr, float* data, int y, float(*fp[])(float*, int) = {lb, ub});
Scheff's Cat
  • 19,528
  • 6
  • 28
  • 56
Codite
  • 37
  • 5
  • I'm unable to copy/paste so I rushed, but its a float – Codite Jan 31 '20 at 06:39
  • What about [edit]? ;-) – Scheff's Cat Jan 31 '20 at 06:41
  • 2
    Btw. it seems to work with a "helper" array: [**Live Demo on coliru**](http://coliru.stacked-crooked.com/a/d9fdbf2c152cf270). – Scheff's Cat Jan 31 '20 at 06:42
  • If I remember right (plain C) arrays cannot be created as temporaries. That might be the reason why it doesn't work without helper array. – Scheff's Cat Jan 31 '20 at 06:43
  • Does this answer your question? [How to pass a temporary array?](https://stackoverflow.com/questions/42332229/how-to-pass-a-temporary-array) – Scheff's Cat Jan 31 '20 at 06:45
  • 1
    @Scheff [I was just thinking the same thing](https://ideone.com/u5DfyP). – Remy Lebeau Jan 31 '20 at 06:46
  • @Scheff Whenever I actually make the function definition a default argument for parameter 4 is given. Is the definition not supposed to match the prototype when using a helper array? – Codite Jan 31 '20 at 07:09
  • I don't get your comment. (Not sure, whether I understood it right.) A default argument is given in declaration (prototype) only. (It must be visible for the source where the function is called.) It may not be repeated in the function definition. (That should result in a compiler error.) Btw. default arguments are applied at "call" time - i.e. at run-time. Hence, even function calls can be used to provide default arguments. – Scheff's Cat Jan 31 '20 at 07:19
  • ...with a scarying function returning a pointer to function pointers: [**Test on coliru**](http://coliru.stacked-crooked.com/a/2327ed62ce3c25ca) ;-) – Scheff's Cat Jan 31 '20 at 07:27
  • array of functioning pointers.. ithis looks like a puzzle (to figure out what really that data type is) or C era legacy XD – Swift - Friday Pie Jan 31 '20 at 08:17

4 Answers4

2

Your array decays to a pointer inside a function declaration (if you don't take it by reference). What you can do is wrap your default value in a constant, like this (note that you lose the array size with this approach if you don't pass it as an extra parameter):

using OperationType = float(*)(float*, int);
constexpr OperationType DefaultOperations[] = {lb, ub};

float operation(Stats* ptr, float* data, int y, const OperationType* fp = DefaultOperations);

If you always know the the size of your array at compile time you can use std::array like this:

using OperationType = float(*)(float*, int);

template <std::size_t Size>
float operation(Stats* ptr, float* data, int y, std::array<OperationType, Size> fp = {lb, ub});
Timo
  • 9,269
  • 2
  • 28
  • 58
1

You are treating the [] as a declaration of an array. But in function parameters it only declares a pointer.

Ruslan
  • 18,162
  • 8
  • 67
  • 136
0

Try this:

typedef float(*fp[])(float*, int);
float operation(Stats* ptr, float* data, int y, fp pfp = (fp) {lb, ub});
Sumit
  • 1,485
  • 11
  • 24
0

Instead of this ugly syntax you could actually use a vector of typedef'd std::functions. It would look better and compile just fine.

#include <functional>
#include <vector>

float lb(float* data, int y) { return 0; }
float ub(float* data, int y) { return 0; }

typedef std::function<float (float*, int)> Func;

float operation(void* ptr, float* data, int y, std::vector<Func> funcs = { lb, ub }) { return 0;  } 
/* didn't declare stats, so I use void here instead */

int main(int argc, char* argv[])
{
    operation(nullptr, nullptr, 0);

    return 0;
}
Leontyev Georgiy
  • 1,295
  • 11
  • 24
  • `std::function` is much more general than a function pointer, and this generality is [not for free](https://stackoverflow.com/q/5057382/673852). – Ruslan Jan 31 '20 at 08:50
  • Of course. Still, unless the code is VERY performance-critical, using std::functions is fine. – Leontyev Georgiy Jan 31 '20 at 08:54
  • I even used them for my ESP32 (which is a microcontroller) projects, and it was fast enough for me. Something like `std::map> network_packet_handler`. – Leontyev Georgiy Jan 31 '20 at 08:58