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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:26:00+00:00 2026-06-01T11:26:00+00:00

Is there a way to, at compile-time, decide on one of two run-time code

  • 0

Is there a way to, at compile-time, decide on one of two run-time code paths? I am aware that function overloading can be used to accomplish this feat, but then the code size increases because both of my functions are compiled and linked into the program. Is there a way to avoid this size overhead?

Essentially, what I want to do is:

#include <boost/mpl/if.hpp>
#include <boost/type_traits/is_abstract.hpp>

template <class T>
    class X
{
    public:
        void copy_t(T &old_t)
        {
            //
            // if T is abstract, (meaning that t is a pointer)
            //
            t = old_t.clone();

            //
            // else
            //
            t = old_t;
        }   

    private: 
        typename boost::mpl::if_<boost::is_abstract<T>, T *, T>::type t;
};

The only way I know involves overloaded member functions:

template <class T>
    class X
{   
    public:
        void copy_t(T &old_t)
        {   
            t = make_copy(old_t, t); 
        }   

    private:
        T *make_copy(T &old_t, T *t) 
        {   
            return old_t.clone();
        }   

        T &make_copy(T &old_t, T &t) 
        {   
            return old_t;
        }   

        typename boost::mpl::if_<boost::is_abstract<T>, T *, T>::type t;
};

But now, two make_copy member functions are compiled and linked into the program, even though X may only ever be instantiated with an abstract class template parameter, in which case, only one of them is needed.

  • 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-01T11:26:01+00:00Added an answer on June 1, 2026 at 11:26 am

    From your examples, it looks like the functions are members of a class
    template. If so, they will only be instantiated if they are actually
    used; if overload resolution always chooses one of them, the other will
    never be instantiated.

    This is a critical rule for a lot of meta-programming techniques. It’s
    not rare in such cases that the function which isn’t instantiated would
    cause compile time errors if it was instantiated.

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

Sidebar

Related Questions

Is there a way I can assert that two constant expressions are equal at
Is there a compile-time way to detect / prevent duplicate values within a C/C++
Is there a way to compile apple script code on the command line from
I doubt if there is a way to make compile-time conditions in Java like
Is there any way to check the format string at compile time ? Example:
Is there a way to get the compile-time byte alignment of a class instance
I've got m2eclipse plugin installed and there's no problem syncing compile time dependencies that
Is there a way to embed a message in code that is displayed during
Is there a safe, portable way to determine (during compile time) the endianness of
Is there any way to validate mappings at compile time? For example, I have

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.