samedi 27 juin 2020

GCC Sees Multiple Arguments Although There is only one

#include <cassert>

template <bool D1, bool ...Ds>
constexpr int reversed_binary_value() {
    return D1 + 2 * reversed_binary_value<Ds...>();
}

template <>
constexpr int reversed_binary_value<0>() {
    return 0;
}

template <>
constexpr int reversed_binary_value<1>() {
    return 1;
}

int main() {
    assert(4 == reversed_binary_value<0, 0, 1>());
}

When I compiled, I got:

test_assert.cpp:19:47: error: macro "assert" passed 3 arguments, but takes just 1
  assert(4 == reversed_binary_value<0, 0, 1>() );
                                               ^
test_assert.cpp: In function ‘int main()’:
test_assert.cpp:19:2: error: ‘assert’ was not declared in this scope
  assert(4 == reversed_binary_value<0, 0, 1>() );
  ^~~~~~
test_assert.cpp:19:2: note: ‘assert’ is defined in header ‘<cassert>’; did you forget to ‘#include <cassert>’?
test_assert.cpp:2:1:
+#include <cassert>
 
test_assert.cpp:19:2:
  assert(4 == reversed_binary_value<0, 0, 1>());
  ^~~~~~

I am using g++ 8.3.1 if it matters.

I am not sure why the compiler sees 3 arguments instead of 1.

Tho, if I change the assertion to

assert(4 == reversed_binary_value<0, 0, 1>());

Then, it compiles fine.

Idea?

Aucun commentaire:

Enregistrer un commentaire