In the following example struct S
inherits from two functional objects A
and B
each with its own operator ()
, and then declares using A::operator()
to take the operator from A
:
using A = decltype([](int){ return 1; });
using B = decltype([](){ return 2; });
struct S : A, B {
using A::operator();
};
int main() {
S s;
static_assert( s() == 2 ); // passes in GCC and Clang, but why?
}
As I expected, this code is rejected by MSVC with the error:
error C2064: term does not evaluate to a function taking 0 arguments
because A::operator(int)
indeed takes 1 argument and B::operator()
shall not be considered.
However both GCC and Clang accept the code and call B::operator()
in static_assert
. Demo: https://gcc.godbolt.org/z/x6x3aWzoq
Which compiler is right here?
static_assert
allowed? Iss()
constexpr
? Why? – Asafoetidaoperator()
is implicitlyconstexpr
since C++17. – Cathryncathyusing A::operator()
. – Cathryncathy