Mapping arrays between C++ and Fortran [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 days ago.
Improve this question
I have a project which successfully maps a C++ structure to a Fortran equivalent. The structure contains arrays. On the C++ side, the arrays in the structure are represented as:
struct t_res_p {
double *p_length = nullptr;
double *p_diam = nullptr;
}
and on the Fortran side:
type :: t_res_p_ext
sequence
real(8), pointer :: p_length
real(8), pointer :: p_diam
end type t_res_p_ext
Which works fine. I have been recommended to use vectors on the C++ side, but when I use this:
struct t_res_p {
CDArray p_length;
CDArray p_diam;
}
( CDArray is std::vector<double> )
The call from C++ is as follows:
t_res_p p_res;
p_res.p_length.SetSize(ns,0.0);
p_model(&p_res);
And on the Fortran side:
subroutine p_model(p_res) bind(C)
type(t_res_p_ext), target, intent(inout) :: p_res
...
The Fortran structure does not match up. Seems like I need to add .data() somewhere although I'm not sure of the syntax.

Related

Cast void pointer to int [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
With a VC++ project I have a line:
FXint realIndex = (int)m_cmbDevice->getItemData(indx);
This reads a void pointer value from combo box to an int value. In VC++ it compiles and works well.
Now I have to port this to Linux and there I get a compile error
cpp:514:20: error:
cast from pointer to smaller type 'int' loses information
FXint realIndex = (int)m_cmbDevice->getItemData(indx);
Now I use
std::size_t x = reinterpret_cast<std::size_t>(m_cmbDevice->getItemData(indx));
FXint realIndex = x;
My question is now, is this the right way to go?
reinterpret_cast is dangerous and is typically used only to interpret a pointer as another kind of pointer. You should use static_cast instead
size_t is not necessarily big enough to store a pointer. There are already intptr_t and uintptr_t which are signed and unsigned integer types that are capable of holding a pointer
FXint realIndex = x; still raises a warning if x is wider than FXint, so another cast is necessary to turn off all related warnings
So you need to do this
auto x = reinterpret_cast<uintptr_t>(m_cmbDevice->getItemData(indx));
auto realIndex = (FXint)x;

Cleaning up a switch statement [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have a container class that contains a char array and an enum/integer indicating what the char array should be cast to. I use it in a callback as so:
void callback(Foo& foo){
switch(foo.type()){
case(1): do( (Bar1*) foo.stuff() );
case(2): do( (Bar2*) foo.stuff() );
case(3): do( (Bar3*) foo.stuff() );
...
}
}
Is there a way to store a mapping from the integer to the type (1, Bar1), (2, Bar2), etc. so that I can clean this switch statement up since it's getting long? Or otherwise, are there any template metaprogramming idioms that can be used in this case?
Store your objects in a std::variant instead of rolling your own using an array of char. Along with std::visit, it supports exactly the use-case you need:
void callback(std::variant<Foo1, Foo2, Foo3>& foo) {
std::visit([](auto& f) { doit(f); }, foo);
}
If you can't use std::variant then a switch is likely the simplest solution given a single known set of types.

Copying Double to a void pointer in C++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I'm copying a double value to a Void*.How can I do it in C++.
Currently I've tried in C++10
strcpy(Trade->MtTr.MPData->MPTrXML.da,dCou);
Trade->MtTr.MPData->MPTrXML.da-->this is a Void*
dCou is double.
strcpy(Trade->MtTr.MPData->MPTrXML.da,dCou);
I expect the void* should contain the double value.
In actual I'm getting error as:
error C2665: 'strcpy' : none of the 2 overloads could convert all the argument types
while trying to match the argument list '(void *, double)'
If you have a valid reason to be attempting to copy a double to a void*, then one way is to use memcpy:
#include <cstring>
//..
// Assuming Trade->MtTr.MPData->MPTrXML.da is valid:
double dCou;
//...
memcpy(Trade->MtTr.MPData->MPTrXML.da, &dCou, sizeof(double));

(C++) What does it mean when a data type is between a '<' and '>' sign? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I'm reading Bjarne Stroustrup's book on C++ and he uses things like vector<int> or complex<double>. What does it mean when a data type like int is between a < and > sign?
Tried Googling but it won't recognize my < or >
They're templates.
Function templates are special functions that can operate with generic types. This allows us to create a function template whose functionality can be adapted to more than one type or class without repeating the entire code for each type.
for example:
template <class C>
C add (C a, C b) {
C result = a + b;
return (result);
}
int a = 1;
int b = 2;
add<int>(a, b); //returns 3
float c = 1.5
float d = 0.5
add<float>(c, d) // returns 2.0

C++ Pointer type cast from class name [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
How to cast a Pointer in C++ from class name in String?
Psuedocode:
int * ptr = something;
myStruct ptrstruct = (ClassFromString("myStruct") ptr);
// The class/struct name is passed in as String
Thank you
I'm not a c++ guru but I have two ideas that may help with brainstorming:
May the use of the registry pattern as described here would be of help: Instantiate class from name?
Secondly, following the registry pattern idea you could crate a function for casting e.g. MyClass something = registry.cast("MyClass", ptr);
I am not sure but this must work
Only Void pointer or boost can help, if it happens
thing * p = something; // pointer to object
void * pv = p; // pointer to void
thing * p2 = static_cast<thing *>(pv); // pointer to the same object
Maybe same kind of situation is while returning values from Threads
Overall reflection is not possible in c++.
Its just brainstroming.