Re: fortran array and std::vector<T>
sebastian sauer wrote:
my case: a C++ std::vector<T> that is also accessed by a fortran subroutine
as an array.
Accessed is a tricky word.
Might you ever call call your fortran code with the data in a
std::vector as a parameter, and then use the fortran to call c++ and, I
don't know, say do something that changes the size of the vector? And
then have the fortran use the values in the array again?
That might be a huge no-no.
my question: how shall i do it?
A way that works.
what is morale?
A way that makes you feel confident.
what is legal?
A way that is approved by the law.
and what is practical?
Following The Way always leads to a practical solution.
Please post follow up to the above questions to a ng that specializes in
practical philosophy. This is the way of comp.lang.c++.
void g()
{
std::vector<Foo> v;
...
f(v.begin(), v.size()); // this is what i cannot do i figure
As has already been pointed out, that won't work.
f(&v[0], v.size()); // this is the yuk-dirty way i came up with
I don't know, and YMMV, but I'd be surprised if the above worked. Perhaps:
int s = v.size();
f(&v[0], &s);
}
please enlighten me what is here the best thing to do?
1) IMHO, the very best thing, assuming funding, is to rewrite all your
code in one language, and use it. I think c++ might be my choice for
that. Yours might not be.
2) Usually (1) is not very practical, so if I could, I'd put all of the
calls to the fortran code in a wrapper. This may add some inefficiency
to the code, perhaps inline would help. But it will isolate the calls
to fortran and save some problems if you ever want to change either one
of your compilers or your linker, because, and this might be important
to you, if not now, then someday:
Mixed Language Programming Is Not Portable!
For that reason, I'd suggest doing something like (untested):
namespace fortran_stuff { // choose a better name
void f(std::vector<Foo> &v) {
int s = v.size();
::f(&v[0], &s);
}
}
Doing this will also make your c++ code more c++ish looking. And save
some of the aggravation of having to deal with creating a variable in
your code for size everytime you call ::f. Or having a variable that
gets used over and over for that. Because you or someone else will
forget and Oops!
LR