There exist many similar questions for C, e.g. this one, but i'm searching for the simplest and most readable way in C++ to pass an C-array into a function without first defining a variable for it.
Given a
void f(double const* a)
these are not possible (but the first one is in C99):
f((double []){1,2}); // works in C
f({1,2}); // would be great but doesn't work in any language with C-arrays
Of course defining a variable first is always possible, but that's not what i'm looking for:
double a[] = { 1.2, 3.4 };
f(a);
However, it is possible to wrap things into a struct, f.e. like
struct A { double a[2]; };
f(A{2.3,4.5}.a);
and that type can be reused, so that's already a little win
but this still feels clumsy.
Is there a simpler and more readable way? In one of the current standards or maybe an upcoming one?
EDIT: Sorry, i didn't mention that before; i can't change the target api. A pointer to a double is expected.
double *[]is a pointer to a pointer todouble, not a poster todouble. No way that's going to match.f((double *[]){1,2});than that's the code from the question about the C-version i linked. I didn't change that code but added it for completeness sake. Is it more intuitive for the question if i remove it or add a title that it's from the C question?chars. Hence, thechar *[]makes sense. If you try to address an array ofdoubles,double *[]is plain wrong - in C as well as C++. Furthermore, I don't think referring to C is of much value here. C and C++ are different languages, and that something is correct in one of them doesn't necessarily mean anything for the other.