Is there any possible way to pass pointer to a double (double *
) as argument to a function which expects a reference to a std::vector<double>
argument in C++11
Asked
Active
Viewed 160 times
1

acraig5075
- 10,588
- 3
- 31
- 50

m.k
- 11
- 1
-
1no, the types mismatch, you need to convert before you call in c++ – Wolfgang Brehm Sep 27 '19 at 08:18
-
2By 'double pointer' do you mean `double*` or a pointer to another pointer? – Peter Hull Sep 27 '19 at 08:21
-
1How would the function know the number of elements that are behind the `double*`? This is quite clearly impossible. – Max Langhof Sep 27 '19 at 08:42
-
Actually i mean pointer to a double (double *) – m.k Sep 27 '19 at 09:03
2 Answers
1
Rather than copying the data into a vector, you could also change the function to take a span
. You will still have to include the length along with the pointer.
void takes_span(std::span<double>) {}
int main()
{
std::vector<double> vec = { 1, 2, 3 };
double arr[3] = { 4, 5, 6 };
double * ptr = arr;
takes_span(vec);
takes_span(arr);
takes_span({ptr, 3});
}

Caleth
- 52,200
- 2
- 44
- 75