Overloading to Enforce C++ Compile-time Constraints

If you ever need to enforce compile-time constraints in C++, here's one way to go about it.


May 18, 2005
URL:http://www.drdobbs.com/overloading-to-enforce-c-compile-time-co/184407752

While reading Chapter One of Matthew Wilson's Imperfect C++ (IC++), I came across those timeless words: "Answers on a postcard, please," as Matthew described an imperfection in a class ancestry compile-time constraint. Confident that the only impossible thing in this world is a young child on e-numbers, I decided that I had to figure out how to solve this problem....

The constraint in question is called must_have_base, which as the name suggests, enforces the fact that one class is derived from another.

The original constraint in IC++, based on a post by Bjarne Stroustrup on comp.lang.c++.moderated and also described as IsDerivedFrom in Herb Sutters' More Exceptional C++, is as follows:

template< typename D // Derived type
        , typename B // Base type
        >
struct must_have_base
{
  ~must_have_base()
  {
    void(*p)(D*, B*) = constraints;
  }
private:
  static void constraints(D* pd, B* pb)
  {
    pb = pd;
  }
};

It takes the address of the constraints method, rather than calling it, to ensure minimal runtime impact, no matter how dense the compiler it's used with. The imperfection in the constraint is that it doesn't prevent the template parameters, D and B, from having the same type.

My solution is to overload the constraints function, with the B and D parameters reversed. If they are of the same type, then the same overload gets defined twice, which is obviously illegal!

The constraints function also gets a name change to cant_be_overloaded_if_same_type, to give more helpful compiler errors in some cases:

 static void cant_be_overloaded_if_same_type(D* pd, B* pb)
  {
    pb = pd;
  }
  static void cant_be_overloaded_if_same_type(B* pb, D* pd)
  {
    pb = pd;
  }

Obviously introducing this change into the extant must_have_base template would run the risk of breaking client code that depends on the old semantics. So it is incorporated into a new constraint, called must_be_derived, which made its debut in the STLSoft libraries in version 1.8.3.

Alas, nothing's ever quite as simple as this: The Borland (C++ Builder 6) compiler takes absolutely no notice of the overload. A bit of poking around revealed that this can be remedied by changing the way that the constraint is enforced in the destructor. If cant_be_overloaded_if_same_type is actually called from the destructor, rather than just taking its address, then all is rosy.

The complete solution is as follows, incorporating the necessary discrimination of functionality for the Borland compiler:

template<   typename D // Derived type
        ,   typename B // Base type
        >
struct must_be_derived
{
public:
    ~must_be_derived()
    {
# if defined(STLSOFT_COMPILER_IS_BORLAND)

        cant_be_overloaded_if_same_type(
                static_cast<D*>(0), 
                static_cast<B*>(0)

# else /* ? compiler */

        void(*p)(D*, B*) = cant_be_overloaded_if_same_type;

        STLSOFT_SUPPRESS_UNUSED(p); // Suppress compiler 'unused variable' warning

# endif /* compiler */
        );
    }

private:
    static void cant_be_overloaded_if_same_type(D* pd, B* pb)
    {
        pb = pd;

        STLSOFT_SUPPRESS_UNUSED(pb); // Suppress compiler 'unused variable' warning
    }
    static void cant_be_overloaded_if_same_type(B* pb, D* pd)
    {                       
        pb = pd;
		
        STLSOFT_SUPPRESS_UNUSED(pb); // Suppress compiler 'unused variable' warning
    }
};

Peter Bannister is a programmer in the UK. He can be contacted at [email protected].

Terms of Service | Privacy Statement | Copyright © 2024 UBM Tech, All rights reserved.