8

Inspired by one of the comments on this question I wanted to write this in my code because I may have made wrong assumptions that would need investigating if I ever port the code to a platform where the two types are not the same.

static_assert(typeid(float) == typeid(GLfloat), "GLfloat is unexpected type");

However that does not compile because error: call to non-constexpr function ‘bool std::type_info::operator==(const std::type_info&) const’

I can however write this :-

static_assert(sizeof(float) == sizeof(GLfloat), "GLfloat is unexpected size");

And it works just as expected. That will most likely be sufficient to give me a compile time error if my assumptions are wrong on a new platform but I was wondering if there was any way to achieve what I really wanted - to compare the actual types?

Community
  • 1
  • 1
jcoder
  • 29,554
  • 19
  • 87
  • 130

1 Answers1

18

Use traits:

#include <type_traits>

static_assert(std::is_same<float, GLfloat>::value, "GLfloat is not float");
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084