Hello,
I have this piece of code: (not written by me)
#include "mpi.h"
// template <size_t>
// struct MPI_Select_unsigned_integer_datatype;
// template <>
// struct MPI_Select_unsigned_integer_datatype<1> {
// static constexpr MPI_Datatype const datatype = MPI_UNSIGNED_CHAR;
// };
int main(int argc, char *argv[])
{
constexpr MPI_Datatype const datatype = MPI_UNSIGNED_CHAR;
return 0;
}
The actual code is the commented out, but the constexpr line produces the
same error.
It compiles fine with g++ 5.1 but fails with clang++ 3.6.1:
% OMPI_CXX="clang++" mpic++ -std=c++11 test.cpp
test.cpp:14:32: error: constexpr variable 'datatype' must be initialized by
a constant expression
constexpr MPI_Datatype const datatype = MPI_UNSIGNED_CHAR;
^ ~~~~~~~~~~~~~~~~~
test.cpp:14:43: note: cast from 'void *' is not allowed in a constant
expression
constexpr MPI_Datatype const datatype = MPI_UNSIGNED_CHAR;
^
/usr/include/mpi.h:1049:27: note: expanded from macro 'MPI_UNSIGNED_CHAR'
#define MPI_UNSIGNED_CHAR OMPI_PREDEFINED_GLOBAL(MPI_Datatype,
ompi_mpi_unsigned_char)
^
/usr/include/mpi.h:308:47: note: expanded from macro
'OMPI_PREDEFINED_GLOBAL'
#define OMPI_PREDEFINED_GLOBAL(type, global) ((type) ((void *) &(global)))
^
1 error generated.
First question: Is the const statement superfluous?
I don't understand why clang fires the error here. MPI_UNSIGNED_CHAR is just
a #define in mpi.h
#define MPI_UNSIGNED_CHAR OMPI_PREDEFINED_GLOBAL(MPI_Datatype,
ompi_mpi_unsigned_char)
which is
#if !OMPI_BUILDING
#define OMPI_PREDEFINED_GLOBAL(type, global) ((type) ((void *) &(global)))
#else
#define OMPI_PREDEFINED_GLOBAL(type, global) ((type) &(global))
#endif
And a bit beyond my understanding of C and C++.
What is the best way to fix compilation with clang?
Is that the same problem?
http://stackoverflow.com/questions/24398102/constexpr-and-initialization-of-a-static-const-void-pointer-with-reinterpret-cas
Thanks!
Florian