lundi 30 janvier 2017

How standards-compliant is my polymorph?

Often in code I write there are types that are layout-compatible but are distinct types, but still I'd like to pass them around as if they were the same type. This comes with quite some syntactical overhead, including the necessary casts etc.

I (very) recently thought up a small helper mix-in class, which I dubbed polymorph:

struct polymorph
{
  template<typename T>
  const T& as() const { return static_cast<const T&>(*this); }
  template<typename T>
  T& as() { return static_cast<const T&>(*this); }
}

My question is: how robust is this class, and how could I make this more resilient against misuse and/or undefined behaviour. I haven't used it a lot and am kind of hesitant because there are a lot of things that could go terribly wrong.

This class is primarily intended for correspondences such as the one between _Complex/std::complex<double>/double[2]. I'm still thinking of a nice way to extend it to perform useful conversions, and exactly how useful that could be.

Aucun commentaire:

Enregistrer un commentaire