
Determining type at run-time or compile-time
What is the best what to determine what a data type is at run-time, or at
compile-time in the preprocessor if possible?
Let me give you the context, so that you know what I am trying to do. I am
writing a program using MPI to manipulate some data. The data is of some
type foo_type which is defined by a typedef in a header file (suppose, for
now, that it is a primitive type). When you send data in MPI, you have to
tell MPI what data type you are sending (this lets it translate the data if
necessary in heterogeneous networks, for example). You do this by passing
an appropriate type constant (MPI_DOUBLE, MPI_FLOAT, MPI_INT, etcetera) to
MPI.
Currently, I am doing something like:
typedef double foo_type;
#define FOO_TYPE_CONST_FOR_MPI MPI_DOUBLE
I want to be able to determine this type constant automatically. For
example, I would like to inherit the type from another header file that
knows nothing about MPI, and ensure that the correct constant is used:
#include "other_header.h"
typedef some_type foo_type;
#define FOO_TYPE_CONST_FOR_MPI ...magic here...
Is there a good way to do this?
Thanks for your suggestions.
Cordially,
Steven G. Johnson
PS. What I would really like is some way to compare types in the
preprocessor. #if types_equal(foo_type,double), and so on. But, it seems
like this isn't possible.
--