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

  • SEARCH
  • Home
  • 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 8732585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:27:57+00:00 2026-06-13T09:27:57+00:00

Im not sure if this is a stupid question, so shoot me if it

  • 0

Im not sure if this is a stupid question, so shoot me if it is!

I am having this “dilemna” which I encounter very often. I have say two overloaded functions in C++

say we have this two overloads of F (just a pseudocode below)

void F(A a, .../*some other parameters*/)
{ 
  //some code part
  //loop Starts here
  G1(a,.../* some other parameter*/)
  //loop ends here
  //some code part
}


void F(B b, .../*some other parameters*/)
{
  //some code part
  //loop Starts here
  G2(b,.../* some other parameter*/)
  //loop ends here
  //some code part
}

where A and B are different types and G1 and G2 are different functions doing different things. The code part of the overloads except for G1 and G2 lines are the same and they are sometimes very long and extensive. Now the question is.. how can I write my code more efficiently. Naturally I want NOT to repeat the code (even if it’s easy to do that, because its just a copy paste routine). A friend suggested macro… but that would look dirty. Is this simple, because if it is Im quite stupid to know right now. Would appreciate any suggestions/help.

Edit: Im sorry for those wanting a code example. The question was really meant to be abstract as I encounter different “similar” situation in which I ask myself how I am able to make the code shorter/cleaner. In most cases codes are long otherwise I wouldn’t bother asking this in the first place. As KilianDS pointed out, it’s also good to make sure that the function itself isn’t very long. But sometimes this is just unavoidable. Many cases where I encounter this, the loop is even nested (i.e. several loops within each other) and the beginning of F we have the start of a loop and the end of F we end that loop.

Jose

  • 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-13T09:27:58+00:00Added an answer on June 13, 2026 at 9:27 am

    The most obvious solution is to put the common code parts into separate
    functions, and call them:

    void F( A a, ... )
    {
        commonPrefix(...);
        G1( a, ... );
        commonPostfix(...);
    }
    
    void F( B b, ... )
    {
        commonPrefix(...);
        G2( a, ... );
        commonPostfix(...);
    }
    

    if there is a lot of data shared between the prefix and the postfix, you
    could create a class to hold it, and make the functions members.

    Alternatively, you could forward to a template, possibly using traits:

    template <typename T>
    class GTraits;
    
    template<>
    class GTraits<A>
    {
        static void doG( A a, ... ) { G1( a, ... ); }
    };
    
    template <>
    class GTraits<B>
    {
        static void doG( B b, ... ) { G2( b, ... ): }
    };
    
    template <typename T>
    void doF( T t, ... )
    {
        //  ...
        GTraits<T>::doG( t, ... );
        //  ...
    }
    
    void F(A a, ...)
    {
        doF( a, ... );
    }
    
    void F(B b, ...)
    {
        doF( b, ... );
    }
    

    This could easily result in the common parts of the code being
    duplicated, however. (Whether this is a problem or not depends. In
    most cases, I suspect that the code bloat would be minimal.)

    EDIT:

    Since you say that the common code includes the loop logic: you can use
    the template method pattern, something like:

    class CommonBase
    {
        virtual void doG( other_params ) = 0;
    public:
        void doF()
        {
            //  prefix
            //  loop start
            doG( other_params );
            //  loop end
            //  suffix
        }
    };
    

    You then define a separate derived class in each function:

    void F( A a ,... )
    {
        class DoG : public CommonBase
        {
            A myA;
            void doG( other_params ) { G1( myA, other_params ); }
        public:
            DoG( A a ) : myA( a ) {}
        } do( a );
        do.doF();
    }
    

    The fact that you need the forwarding constructor makes it a bit wordy,
    but it does keep all of the common code common.

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

Sidebar

Related Questions

This is probably a very stupid question, but I am just not sure which
I am not sure if this is a stupid question or not. I have
I'm not very good at this, so I'm sure this is a stupid question.
I'm not sure if this might be a rather stupid question. But is it
This may be a stupid question, as I'm not sure how MSBuild works with
I'm sure this is a stupid question, but why does the following code not
this is a really stupid question but I have a deadline and I'm very
So this is probably a stupid question, but I am still not exactly sure
I'm not really sure how to ask this question. Suppose I have a class
Now, I'm not sure whether this is a stupid question, please bear with me

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.