static_assert unexpected behavior

I have an overloaded operator& for my class where I do a static assert if the parameter is a pointer.

class test {
public:

  template<typename T>
  friend inline test& operator&(test& so, T const& t) {
    std::cout << "is_pointer : " << std::is_pointer<T>::value << std::endl;
    static_assert(std::is_pointer<T>::value, "no operator overloaded for pointers.");
    
    // some stuff
  }
};

If I use this operator I always get the assertion even if the type is definitively not a pointer. std::cout << "is_pointer : " << std::is_pointer<T>::value << std::endl; is printing a zero...

int main() {
  test t;
  t & 123;
  return 0;
}

Example.


Of course the assertion fails. You're requiring that the type T is a pointer, but T here is an int.

Perhaps you meant the following?

// Ensure that `T` is not a pointer
static_assert(!std::is_pointer<T>::value, "no operator overloaded for pointers");

Assertions are statements that ensure a particular condition. It's not quite "if X then output error message Y"; in fact, it's the exact opposite. :)


You are asserting that the type passed is a pointer. If you pass something which is not a pointer, the static_assert() fails and you get a message. It seems, you want to precisely negate the condition as you do not want to work with pointers:

static_assert(!std::is_pointer<T>::value, "no operator overloaded for pointers.");