Remix.run Logo
humanrebar 3 days ago

You can implement that in C++ code, no language extensions required, by using type erasure and a pointer semantic type with at least one constructor templated on the type of the object being pointed to.

kazinator 3 days ago | parent [-]

A signature pointer type knows nothing about the types it is legally pointing to; moreover, those types know nothing about the signature.

  signature communicator {
    size_t send(const unsigned char *buf, size_t size);
    size_t recv(const unsigned char *buf, size_t size);
  };

  class foo {
    size_t send(const unsigned char *buf, size_t size);
    size_t recv(const unsigned char *buf, size_t size);
  };

  foo f;
  communicator *c = &f; // valid: foo conforms to communicator signature
(Not sure if these member functions need to be virtual? I would have to dig up the Signatures docs.)

This feature was removed because the implementation was becoming hard to maintain, or some reason like that.

You can see it's pretty crazy from a C++ point of view, because c->recv(...) can be called through this pointer and it has to work for absolutely any object whose class conforms to the signature. And classes don't know they are conforming to any signature; nothing is declared in them for that.

C++ polymorphism normally depends on declaration relationships through which implementation details like vtable positions can be inferred.

saurik 3 days ago | parent [-]

But yet it is still true that you can implement this idea in normal C++ the way humanrebar described. (This is similar to std::function, which is a pointer to an object that happens to have an operator() that conforms to listed signature; but you can generalize that to any other set of things you want to be able to abstract into the pointer.)