Search code examples
c++gcctemplate-specializationtypeof

Combining GCC's typeof() extension with member access


I'm working on a C++ library that uses static polymorphism via templates. It is designed this way because the target is an embedded system with a small stack and often it is beneficial to inline member functions to save on stack usage.

The use of templates for static polymorphism means that the names of the types of the event emitters (most often device drivers) are often obnoxiously long:

class DeviceThatUsesSPI<class SPI_BUS_TYPE> {

  public:
    class DeviceEvent : public Event<DeviceThatUsesSPI> {};

    // and the rest of the device driver implementation, including
    // the code that emits that event.

}

SomeSpecificGpioBus gpio_bus();
SoftwareSpiBus<typeof(gpio_bus)> spi_bus(&gpio_bus);
DeviceThatUsesSPI<typeof(spi_bus)> device(&spi_bus);

As you can see, we use the GCC typeof extension operator to avoid writing out the full obnoxious type name DeviceThatUsesSPI<SoftwareSpiBus<SomeSpecificGpioBus>> repeatedly. This has worked like a charm everywhere we've tried it until today I tried to use it to access a nested class representing an event. In my current example this is a template specialization, implementing compile-time event handler binding:

template<>
inline void event_handler<typeof(device)::ExampleEvent>(typeof(device) *emitter) {
    // event handler implementation...
}

However, I've also tried this in a much more minimal example of a variable declaration:

typeof(device)::ExampleEvent event;

In both cases G++ fails to parse the expression with a syntax error. I assume this is because in the standard C++ grammar there is no situation where a :: follows anything except an identifier and the parser is unable to backtrack and treat the first part as a type when it encounters the colons.

However, the GCC manual about typeof makes the following promise about this operator:

A typeof construct can be used anywhere a typedef name can be used. For example, you can use it in a declaration, in a cast, or inside of sizeof or typeof.

If I replace the two uses of typeof in my example with a typedef, G++ is happy:

typedef typeof(device) device_type;

template<>
inline void event_handler<device_type::ExampleEvent>(typeof(device) *emitter) {
    // event handler implementation...
}

device_type::ExampleEvent event;

So this furthers my suspicion that the compiler is fine with what I wrote semantically but the grammar doesn't allow me to express it. While using the typedef indirection does get me working code, I'd prefer to find a way to make the event handler declarations self-contained for convenience to the users of this library. Is there some way to write the typeof operator to remove the parsing ambiguity so that I can make the event declaration a one-liner?


Solution

  • I think a small meta-function can do the trick.

    template<typename T>
    struct self
    {
       typedef T type;
    };
    

    then use it as:

    template<>
    inline void 
    event_handler<self<typeof(device)>::type::ExampleEvent>(typeof(device) *emitter)
    {
        // event handler implementation...
    }
    

    Or you can define the meta-function (which is less generic) as:

    template<typename T>
    struct event
    {
       typedef typename T::ExampleEvent type;
    };
    

    then use it as:

    template<>
    inline void 
    event_handler<event<typeof(device)>::type>(typeof(device) *emitter)
    {
        // event handler implementation...
    }
    

    By the way, in C++11, you can use decltype instead of typeof(which is a compiler extension):

    template<>
    inline void 
    event_handler<decltype(device)::ExampleEvent>(typeof(device) *emitter) 
    {
        // event handler implementation...
    }
    

    Hope that helps. :-)