0

I was trying to use std::is_same to verify the underlying type of a strongly typed enum and I noticed a strange situation where I needed to use double parentheses, but I don't understand why. I've reduced the example down to as follows:

#include <type_traits>
#include <cassert>
#include <stdint.h>

int main(int argc, char *argv[])
{
    assert((std::is_same<unsigned int,uint32_t>::value == true)); // OK
    assert((std::is_same<unsigned int,uint32_t>::value) == true); // OK
    //assert(std::is_same<unsigned int,uint32_t>::value == true); // Compile error
    static_assert(std::is_same<unsigned int,uint32_t>::value == true, "BAD"); // OK
    return 0;
}

Compile error:

isSameAssert.cpp:9:62: error: macro "assert" passed 2 arguments, but takes just 1
     assert(std::is_same<unsigned int,uint32_t>::value == true); // Compile error
                                                              ^
isSameAssert.cpp: In function ‘int main(int, char**)’:
isSameAssert.cpp:9:5: error: ‘assert’ was not declared in this scope
     assert(std::is_same<unsigned int,uint32_t>::value == true); // Compile error
     ^
make: *** [build/isSameAssert] Error 1

Could anyone explain this or point me to a reference that does?

austinmarton
  • 2,278
  • 3
  • 20
  • 23

1 Answers1

4

Because assert is a macro, the expression assert(std::is_same<unsigned int,uint32_t>::value == true); seems to call assert with two parameters due to the comma between int and uint32_t so the compiler complains that assert takes only one parameter, but two were supplied.

Indeed, putting it again in parenthesis solves this issue.

Ferenc Deak
  • 34,348
  • 17
  • 99
  • 167