proxygen
`folly/Poly.h`

Poly is a class template that makes it relatively easy to define a type-erasing polymorphic object wrapper.

Type-erasure


std::function is one example of a type-erasing polymorphic object wrapper; folly::exception_wrapper is another. Type-erasure is often used as an alternative to dynamic polymorphism via inheritance-based virtual dispatch. The distinguishing characteristic of type-erasing wrappers are:

  • Duck typing: Types do not need to inherit from an abstract base class in order to be assignable to a type-erasing wrapper; they merely need to satisfy a particular interface.
  • Value semantics: Type-erasing wrappers are objects that can be passed around by value. This is in contrast to abstract base classes which must be passed by reference or by pointer or else suffer from slicing, which causes them to lose their polymorphic behaviors. Reference semantics make it difficult to reason locally about code.
  • Automatic memory management: When dealing with inheritance-based dynamic polymorphism, it is often necessary to allocate and manage objects on the heap. This leads to a proliferation of shared_ptrs and unique_ptrs in APIs, complicating their point-of-use. APIs that take type-erasing wrappers, on the other hand, can often store small objects in-situ, with no dynamic allocation. The memory management, if any, is handled for you, and leads to cleaner APIs: consumers of your API don't need to pass shared_ptr<AbstractBase>; they can simply pass any object that satisfies the interface you require. (std::function is a particularly compelling example of this benefit. Far worse would be an inheritance-based callable solution like shared_ptr<ICallable<void(int)>>. )

Examples: Defining a type-erasing function wrapper with folly::Poly


Defining a polymorphic wrapper with Poly is a matter of defining two things:

  • An interface, consisting of public member functions, and
  • A mapping from a concrete type to a set of member function bindings.

Below is a simple example program that defines a drawable wrapper for any type that provides a draw member function. (The details will be explained later.)

1 // This example is an adaptation of one found in Louis Dionne's dyno library.
2 #include <folly/Poly.h>
3 #include <iostream>
4 
5 struct IDrawable {
6  // Define the interface of something that can be drawn:
7  template <class Base> struct Interface : Base {
8  void draw(std::ostream& out) const { folly::poly_call<0>(*this, out);}
9  };
10  // Define how concrete types can fulfill that interface (in C++17):
11  template <class T> using Members = folly::PolyMembers<&T::draw>;
12 };
13 
14 // Define an object that can hold anything that can be drawn:
15 using drawable = folly::Poly<IDrawable>;
16 
17 struct Square {
18  void draw(std::ostream& out) const { out << "Square\n"; }
19 };
20 
21 struct Circle {
22  void draw(std::ostream& out) const { out << "Circle\n"; }
23 };
24 
25 void f(drawable const& d) {
26  d.draw(std::cout);
27 }
28 
29 int main() {
30  f(Square{}); // prints Square
31  f(Circle{}); // prints Circle
32 }

The above program prints:

1 Square
2 Circle

Here is another (heavily commented) example of a simple implementation of a std::function-like polymorphic wrapper. Its interface has only a single member function: operator()

1 // An interface for a callable object of a particular signature, Fun
2 // (most interfaces don't need to be templates, FWIW).
3 template <class Fun>
4 struct IFunction;
5 
6 template <class R, class... As>
7 struct IFunction<R(As...)> {
8  // An interface is defined as a nested class template called
9  // Interface that takes a single template parameter, Base, from
10  // which it inherits.
11  template <class Base>
12  struct Interface : Base {
13  // The Interface has public member functions. These become the
14  // public interface of the resulting Poly instantiation.
15  // (Implementation note: Poly<IFunction<Sig>> will publicly
16  // inherit from this struct, which is what gives it the right
17  // member functions.)
18  R operator()(As... as) const {
19  // The definition of each member function in your interface will
20  // always consist of a single line dispatching to folly::poly_call<N>.
21  // The "N" corresponds to the N-th member function in the
22  // list of member function bindings, Members, defined below.
23  // The first argument will always be *this, and the rest of the
24  // arguments should simply forward (if necessary) the member
25  // function's arguments.
26  return static_cast<R>(
27  folly::poly_call<0>(*this, std::forward<As>(as)...));
28  }
29  };
30  // The "Members" alias template is a comma-separated list of bound
31  // member functions for a given concrete type "T". The
32  // "FOLLY_POLY_MEMBERS" macro accepts a comma-separated list, and the
33  // (optional) "FOLLY_POLY_MEMBER" macro lets you disambiguate overloads
34  // by explicitly specifying the function signature the target member
35  // function should have. In this case, we require "T" to have a
36  // function call operator with the signature `R(As...) const`.
37  //
38  // If you are using a C++17-compatible compiler, you can do away with
39  // the macros and write this as:
40  //
41  // template <class T>
42  // using Members =
43  // folly::PolyMembers<folly::sig<R(As...) const>(&T::operator())>;
44  //
45  // And since `folly::sig` is only needed for disambiguation in case of
46  // overloads, if you are not concerned about objects with overloaded
47  // function call operators, it could be further simplified to:
48  //
49  // template <class T>
50  // using Members = folly::PolyMembers<&T::operator()>;
51  //
52  template <class T>
53  using Members = FOLLY_POLY_MEMBERS(
54  FOLLY_POLY_MEMBER(R(As...) const, &T::operator()));
55 };
56 
57 // Now that we have defined the interface, we can pass it to Poly to
58 // create our type-erasing wrapper:
59 template <class Fun>
60 using Function = Poly<IFunction<Fun>>;

Given the above definition of Function, users can now initialize instances of (say) Function<int(int, int)> with function objects like std::plus<int> and std::multiplies<int>, as below:

1 Function<int(int, int)> fun = std::plus<int>{};
2 assert(5 == fun(2, 3));
3 fun = std::multiplies<int>{};
4 assert(6 = fun(2, 3));

Defining an interface with C++17


With C++17, defining an interface to be used with Poly is fairly straightforward. As in the Function example above, there is a struct with a nested Interface class template and a nested Members alias template. No macros are needed with C++17.

Imagine we were defining something like a Java-style iterator. If we are using a C++17 compiler, our interface would look something like this:

1 template <class Value>
2 struct IJavaIterator {
3  template <class Base>
4  struct Interface : Base {
5  bool Done() const { return folly::poly_call<0>(*this); }
6  Value Current() const { return folly::poly_call<1>(*this); }
7  void Next() { folly::poly_call<2>(*this); }
8  };
9  // NOTE: This works in C++17 only:
10  template <class T>
11  using Members = folly::PolyMembers<&T::Done, &T::Current, &T::Next>;
12 };
13 
14 template <class Value>
15 using JavaIterator = Poly<IJavaIterator<Value>>;

Given the above definition, JavaIterator<int> can be used to hold instances of any type that has Done, Current, and Next member functions with the correct (or compatible) signatures.

The presence of overloaded member functions complicates this picture. Often, property members are faked in C++ with const and non-const member function overloads, like in the interface specified below:

1 struct IIntProperty {
2  template <class Base>
3  struct Interface : Base {
4  int Value() const { return folly::poly_call<0>(*this); }
5  void Value(int i) { folly::poly_call<1>(*this, i); }
6  };
7  // NOTE: This works in C++17 only:
8  template <class T>
9  using Members = folly::PolyMembers<
10  folly::sig<int() const>(&T::Value),
11  folly::sig<void(int)>(&T::Value)>;
12 };
13 
14 using IntProperty = Poly<IIntProperty>;

Now, any object that has Value members of compatible signatures can be assigned to instances of IntProperty object. Note how folly::sig is used to disambiguate the overloads of &T::Value.

Defining an interface with C++14


In C++14, the nice syntax above doesn't work, so we have to resort to macros. The two examples above would look like this:

1 template <class Value>
2 struct IJavaIterator {
3  template <class Base>
4  struct Interface : Base {
5  bool Done() const { return folly::poly_call<0>(*this); }
6  Value Current() const { return folly::poly_call<1>(*this); }
7  void Next() { folly::poly_call<2>(*this); }
8  };
9  // NOTE: This works in C++14 and C++17:
10  template <class T>
11  using Members = FOLLY_POLY_MEMBERS(&T::Done, &T::Current, &T::Next);
12 };
13 
14 template <class Value>
15 using JavaIterator = Poly<IJavaIterator<Value>>;

and

1 struct IIntProperty {
2  template <class Base>
3  struct Interface : Base {
4  int Value() const { return folly::poly_call<0>(*this); }
5  void Value(int i) { return folly::poly_call<1>(*this, i); }
6  };
7  // NOTE: This works in C++14 and C++17:
8  template <class T>
9  using Members = FOLLY_POLY_MEMBERS(
10  FOLLY_POLY_MEMBER(int() const, &T::Value),
11  FOLLY_POLY_MEMBER(void(int), &T::Value));
12 };
13 
14 using IntProperty = Poly<IIntProperty>;

Extending interfaces


One typical advantage of inheritance-based solutions to runtime polymorphism is that one polymorphic interface could extend another through inheritance. The same can be accomplished with type-erasing polymorphic wrappers. In the Poly library, you can use folly::PolyExtends to say that one interface extends another.

1 struct IFoo {
2  template <class Base>
3  struct Interface : Base {
4  void Foo() const { return folly::poly_call<0>(*this); }
5  };
6  template <class T>
7  using Members = FOLLY_POLY_MEMBERS(&T::Foo);
8 };
9 
10 // The IFooBar interface extends the IFoo interface
11 struct IFooBar : PolyExtends<IFoo> {
12  template <class Base>
13  struct Interface : Base {
14  void Bar() const { return folly::poly_call<0>(*this); }
15  };
16  template <class T>
17  using Members = FOLLY_POLY_MEMBERS(&T::Bar);
18 };
19 
20 using FooBar = Poly<IFooBar>;

Given the above definition, instances of type FooBar have both Foo() and Bar() member functions.

The sensible conversions exist between a wrapped derived type and a wrapped base type. For instance, assuming IDerived extends IBase with PolyExtends:

1 Poly<IDerived> derived = ...;
2 Poly<IBase> base = derived; // This conversion is OK.

As you would expect, there is no conversion in the other direction, and at present there is no Poly equivalent to dynamic_cast.

Type-erasing polymorphic reference wrappers


Sometimes you don't need to own a copy of an object; a reference will do. For that you can use Poly to capture a reference to an object satisfying an interface rather than the whole object itself. The syntax is intuitive.

1 int i = 42;
2 
3 // Capture a mutable reference to an object of any IRegular type:
4 Poly<IRegular &> intRef = i;
5 
6 assert(42 == folly::poly_cast<int>(intRef));
7 // Assert that we captured the address of "i":
8 assert(&i == &folly::poly_cast<int>(intRef));

A reference-like Poly has a different interface than a value-like Poly. Rather than calling member functions with the obj.fun() syntax, you would use the obj->fun() syntax. This is for the sake of const-correctness. For example, consider the code below:

1 struct IFoo {
2  template <class Base>
3  struct Interface {
4  void Foo() { folly::poly_call<0>(*this); }
5  };
6  template <class T>
7  using Members = folly::PolyMembers<&T::Foo>;
8 };
9 
10 struct SomeFoo {
11  void Foo() { std::printf("SomeFoo::Foo\n"); }
12 };
13 
14 SomeFoo foo;
15 Poly<IFoo &> const anyFoo = foo;
16 anyFoo->Foo(); // prints "SomeFoo::Foo"

Notice in the above code that the Foo member function is non-const. Notice also that the anyFoo object is const. However, since it has captured a non-const reference to the foo object, it should still be possible to dispatch to the non-const Foo member function. When instantiated with a reference type, Poly has an overloaded operator-> member that returns a pointer to the IFoo interface with the correct const-ness, which makes this work.

The same mechanism also prevents users from calling non-const member functions on Poly objects that have captured const references, which would violate const-correctness.

Sensible conversions exist between non-reference and reference Polys. For instance:

1 Poly<IRegular> value = 42;
2 Poly<IRegular &> mutable_ref = value;
3 Poly<IRegular const &> const_ref = mutable_ref;
4 
5 assert(&poly_cast<int>(value) == &poly_cast<int>(mutable_ref));
6 assert(&poly_cast<int>(value) == &poly_cast<int>(const_ref));

Non-member functions (C++17)


If you wanted to write the interface ILogicallyNegatable, which captures all types that can be negated with unary operator!, you could do it as we've shown above, by binding &T::operator! in the nested Members alias template, but that has the problem that it won't work for types that have defined unary operator! as a free function. To handle this case, the Poly library lets you use a free function instead of a member function when creating a binding.

With C++17 you may use a lambda to create a binding, as shown in the example below:

1 struct ILogicallyNegatable {
2  template <class Base>
3  struct Interface : Base {
4  bool operator!() const { return folly::poly_call<0>(*this); }
5  };
6  template <class T>
7  using Members = folly::PolyMembers<
8  +[](T const& t) -> decltype(bool(!t)) { return bool(!t); }>;
9 };

This requires some explanation. The unary operator+ in front of the lambda is necessary! It causes the lambda to decay to a C-style function pointer, which is one of the types that folly::PolyMembers accepts. The decltype in the lambda return type is also necessary. Through the magic of SFINAE, it will cause Poly<ILogicallyNegatable> to reject any types that don't support unary operator!.

If you are using a free function to create a binding, the first parameter is implicitly the this parameter. It will receive the type-erased object.

Non-member functions (C++14)


If you are using a C++14 compiler, the definition of ILogicallyNegatable above will fail because lambdas are not constexpr. We can get the same effect by writing the lambda as a named free function, as show below:

1 struct ILogicallyNegatable {
2  template <class Base>
3  struct Interface : Base {
4  bool operator!() const { return folly::poly_call<0>(*this); }
5  };
6  template <class T>
7  static auto negate(T const& t)
8  -> decltype(bool(!t)) { return bool(!t); }
9  template <class T>
10  using Members = FOLLY_POLY_MEMBERS(&negate<T>);
11 };

As with the example that uses the lambda in the preceding section, the first parameter is implicitly the this parameter. It will receive the type-erased object.

Multi-dispatch


What if you want to create an IAddable interface for things that can be added? Adding requires two objects, both of which are type-erased. This interface requires dispatching on both objects, doing the addition only if the types are the same. For this we make use of the PolySelf template alias to define an interface that takes more than one object of the the erased type.

1 struct IAddable {
2  template <class Base>
3  struct Interface : Base {
4  friend PolySelf<Base>
5  operator+(PolySelf<Base> const& a, PolySelf<Base> const& b) const {
6  return folly::poly_call<0>(a, b);
7  }
8  };
9  template <class T>
10  using Members = folly::PolyMembers<
11  +[](T const& a, T const& b) -> decltype(a + b) { return a + b; }>;
12 };

Given the above definition of IAddable we would be able to do the following:

1 Poly<IAddable> a = 2, b = 3;
2 Poly<IAddable> c = a + b;
3 assert(poly_cast<int>(c) == 5);

If a and b stored objects of different types, a BadPolyCast exception would be thrown.

Move-only types


If you want to store move-only types, then your interface should extend the poly::IMoveOnly interface.

Implementation notes


Poly will store "small" objects in an internal buffer, avoiding the cost of of dynamic allocations. At present, this size is not configurable; it is pegged at the size of two doubles.

Poly objects are always nothrow movable. If you store an object in one that has a potentially throwing move constructor, the object will be stored on the heap, even if it could fit in the internal storage of the Poly object. (So be sure to give your objects nothrow move constructors!)

Poly implements type-erasure in a manner very similar to how the compiler accomplishes virtual dispatch. Every Poly object contains a pointer to a table of function pointers. Member function calls involve a double- indirection: once through the v-pointer, and other indirect function call through the function pointer.