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 9228617
In Process

The Archive Base Latest Questions

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

I have a proprietary MCU that does a periodic task, and wish to fill

  • 0

I have a proprietary MCU that does a periodic task, and wish to fill its “downtime” with other things to do. Typically, this has been done with large switch statements, which is okay, but I’m wondering if there’s a more elegant way. This is a very common pattern in the code for this particular device, so having a generic method would be nice.

So I wrote the following code, and it works, but it doesn’t currently inline the functions.

    static InterlacedFunction searchFunctions[4] = {...};

    typedef int (* const InterlacedFunction)(int);

    template<const int numberOfWovenFunctions> int SendPacketWithWovenFunctions(
            int * packet,
            const int packetLength,
            InterlacedFunction (&functions)[numberOfWovenFunctions],
            int firstArgument = 0)
    {
            int returnFromLastWoven = (numberOfWovenFunctions != 0) ? (functions[0])(firstArgument) : 0;

            SendData(packet[0]);

            for(int i = 1; i < packetLength; i++)
            {
                    if(i < numberOfWovenFunctions)
                            returnFromLastWoven = (functions[i])(returnFromLastWoven);
                    SendData(packet[i]);
            }
            return returnFromLastWoven;
    }

Am I missing something, is it impossible for Clang to inline these functions or does Clang just not have the optimization yet?

  • 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:22:07+00:00Added an answer on June 18, 2026 at 5:22 am

    In general compilers are not aggressive at inlining calls through function pointers, even when they are known at compile time.

    In this case you are relying on the compiler being clever enough to unroll the loop into a block of numberOfWovenFunctions iterations, or generate a switch of the inlined functions for you, both of which are unlikely.

    If you wanted to make the idiom more generic you could do so with a recursive templates (it would be a pain to write if you do not have c++11 variadic templates), though whether it actually constitutes a simplification is perhaps questionable.

    i.e. you template on the list of woven function pointer values, and at each level call the woven function, store the result, call sendPacket and the recurse into the next template level with the stored result. Something like (untested):

    template <InterlacedFunction ... args>
    struct WovenFunc;
    
    //Base case - send remaining packets
    template <> 
    struct WovenFunc<>{
        static int call(int value, int * packet, size_t count){
            for(size_t c = 0; c < count; ++c) SendData(packet[c]);
            return value;
        }
    };
    
    //Recursive case - send packets + weave functions
    template <InterlacedFunction arg, InterlacedFunction ... args>
    struct WovenFunc<arg, args...>{
        static int call(int initial, int * packets, size_t count){
            int r = arg(initial);
            SendData(packets[0]);
            if(count)
                return WovenFunc<args...>::call(r, packets + 1, count - 1);
        }
    };
    

    And then in the send:

    typedef WovenFunc<Woven1, Woven2, Woven3> WovenSend
    WovenSend::call(returnFromLastWoven, packets, packetLength);
    

    Obviously you can make is more generic.

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

Sidebar

Related Questions

Ok, i have a simple database engine (its a proprietary product written in vb6)
I have some code that uses the proprietary sun.*.OperatingSystemMXBean, so I was being careful
I have a class, and list of instances, that looks something like this (field
I have several proprietary IntelliJ plugins that I am hosting on an Enterprise Repository.
I'm working on a proprietary feedback application. I have a table named topics that
I have some nicely-structured data that looks like this: CREATE TABLE SourceBodyPartColors ( person_ID
We have a proprietary web app that has a flowery admin UI, but poor
I have a proprietary application and its war file is available to us. I
Okay so i have a packed a proprietary binary format. That is basically a
So i have the misfortune of embedding this proprietary ActiveX control we created into

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.