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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:07:16+00:00 2026-05-23T14:07:16+00:00

I have a highly polymorphic c++ function that can be called with nearly 20

  • 0

I have a highly polymorphic c++ function that can be called with nearly 20 diferent types.

In order to expose it to python I’m doing something like this:

#include originalFunctionNamespace.hpp

template<class T>
    T foo(T x)
    {
        return orignalFunctionNamespace::foo(x);
    }

but then to call them from python I have to specialize the functions with each type they support:

BOOST_PYTHON_MODULE(module_foo)
{
    def("foo", foo<orignalFunctionNamespace::type1>);
    def("foo", foo<orignalFunctionNamespace::type2>);
    def("foo", foo<orignalFunctionNamespace::type3>);
    def("foo", foo<orignalFunctionNamespace::type4>);
    def("foo", foo<orignalFunctionNamespace::type5>);
    def("foo", foo<orignalFunctionNamespace::type6>);
    def("foo", foo<orignalFunctionNamespace::type7>);
    ...
    ...
    ...
    def("foo", foo<orignalFunctionNamespace::typeN>);
}

This works, but I can’t stop thinking that there must be a smarter way to do it. Since I have to do it for many many functions, things are getting big and super repetitive.
Any suggestions?

  • 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-05-23T14:07:17+00:00Added an answer on May 23, 2026 at 2:07 pm

    Looks like a possible use for typelist magic, to me.

    If you’re using (or can use) the new C++0x standard, you can write something like this:

    template <typename ArgType>
    void def_foo_overloads()
    {
        def("foo", foo<ArgType>);
        // last type in list
    }
    template <typename ArgType, typename... MoreArgTypes>
    void def_foo_overloads()
    {
        def("foo", foo<ArgType>);
        def_foo_overloads(MoreArgTypes...);
    }
    
    // use it like:
    def_foo_overloads<orignalFunctionNamespace::type1,
                      orignalFunctionNamespace::type2,
                      ...
                      orignalFunctionNamespace::typeN> ();
    

    If you don’t have access to the new standard, you can do the equivalent thing using the old recursive typelists.

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

Sidebar

Related Questions

I have some HIGHLY sensitive data that I need to expose to Android and
I have a highly complicated web site that is under source control and has
I have the following (highly simplified) XML document that I am reading into my
I have to write a program that performs highly computationally intensive calculations. The program
I have a computational intensive project that is highly parallelizable: basically, I have a
Out of order execution in CPUs means that a CPU can reorder instructions to
i have a highly data computation intensive application that has to process data received
Is there a way to have one container div that I can put links
In a Python Google App Engine environment, I have some highly volatile data which
I have been reading this ebook about DDD and it says that only highly

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.