Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9230553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:51:13+00:00 2026-06-18T05:51:13+00:00

I’m working on a C++ library that uses static polymorphism via templates. It is

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T05:51:14+00:00Added an answer on June 18, 2026 at 5:51 am

    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. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.