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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:28:51+00:00 2026-06-03T13:28:51+00:00

I am writing a meta function replace_type<C, X, Y> that is supposed to replace

  • 0

I am writing a meta function replace_type<C, X, Y> that is supposed to replace all matches of type X in a compound type C with Y. I am currently working on properly getting this to work with callables in C.

This works:

template replace_type<
    typename C, typename X, typename Y,
    typename First
>
struct replace_type<C(First), X, Y>
{
    typedef typename replace_type<
        C, X, Y
    >::type type(
        typename replace_type<
            First, X, Y
        >::type
    );
};

template replace_type<
    typename C, typename X, typename Y,
    typename First, typename Second
>
struct replace_type<C(First, Second), X, Y>
{
    typedef typename replace_type<
        C, X, Y
    >::type type(
        typename replace_type<
            First, X, Y
        >::type,
        typename replace_type<
            Second, X, Y
        >::type
    );
};

But this obviously is very limited. In my head it seemed obvious that I should be using a variadic template here instead, but when I actually tried applying it, I quickly noticed I have no idea of how to fit it into this scheme.

I thought of implementing it like this:

template replace_type<
    typename C, typename X, typename Y,
    typename First, typename... Args
>
struct replace_type<C(First, Args...), X, Y>
{
    typedef typename replace_type<
        C, X, Y
    >::type type(
        typename replace_type<
            First, X, Y
        >::type,
        // How to recursively do the same with the rest of the arguments?
    );
};

This way, I could always access the first parameter to replace it appropriately, then move on to the next, and have another specialized metafunction ready for handling nullary functions as terminal condition for my recursion. Question is, as stated in the source code, how do I start the recursion in this context?

Update

Minimal example:

#include <type_traits>

namespace type_replace_helper
{
    template <typename, typename, typename>
    struct type_replace_base;
}

template <typename C, typename X, typename Y>
struct type_replace
{
    typedef typename std::conditional<
        std::is_same<C, X>::value,
        Y,
        typename type_replace_helper::type_replace_base<
            C, X, Y
        >::type
    >::type type;
};

namespace type_replace_helper
{
    template <typename C, typename X, typename Y>
    struct type_replace_base
    {
        typedef C type;
    };

    template <typename C, typename X, typename Y>
    struct type_replace_base<C(), X, Y>
    {
        typedef typename type_replace<
            C, X, Y
        >::type type();
    };

    template <
        typename C, typename X, typename Y,
        typename First
    >
    struct type_replace_base<C(First), X, Y>
    {
        typedef typename type_replace<
            C, X, Y
        >::type type(
            typename type_replace<
                First, X, Y
            >::type
        );
    };

    template <
        typename C, typename X, typename Y,
        typename First, typename Second
    >
    struct type_replace_base<C(First, Second), X, Y>
    {
        typedef typename type_replace<
            C, X, Y
        >::type type(
            typename type_replace<
                First, X, Y
            >::type,
            typename type_replace<
                Second, X, Y
            >::type
        );
    };
}

int main()
{
    static_assert(std::is_same<
        type_replace<int(int, int), int, long>::type,
        long(long, long)
    >::value, "int should be replaced by long");
    return 0;
}

Update 2

Thanks to Crazy Eddie I’ve been able to achieve what I wanted. Because it took me so long to understand this beast answer, I figured it might be helpful for others to read a more verbose solution.

The thing I took probably the longest to actually realize: the problem is not really how to separate the function parameters, but transform them into a variadic list of replaced arguments. Thus, the primary objective here is to find a way to replace each argument properly, and store it into another separate argument list. Eddy’s solution uses the stack structure as a wrapper to distinct the two parameter lists, one replaced, one with things left to do.

Once the parameter list has been replaced one by one and was stored in the stack structure, all that’s left to do is pull them out again as a list and construct the function like so: typedef T type(Params...);, and that’s it.

In my coding style this displays as:

template <typename...>
struct stack {};

// Definition only to specialize for actual stacks
template <
    typename X, typename Y,
    typename Stack, typename... Todo
>
struct list_converter;

// No more arguments to convert, return the gathered stack
template <
    typename X, typename Y,
    typename... Elems
>
struct list_converter<X, Y, stack<Elems...>>
{
    typedef stack<Elems...> type;
};

// Push replaced argument to stack and go to the next argument
template <
    typename X, typename Y,
    typename... Elems,
    typename First, typename... Todo
>
struct list_converter<X, Y, stack<Elems...>, First, Todo...>
{
    typedef typename list_converter<
        X, Y,
        stack<
            typename replace_type<First, X, Y>::type,
            Elems...
        >,
        Todo...
    >::type type;
};

// Definition only again for stack specialization
template <
    typename C, typename X, typename Y,
    typename Stack
>
struct function_builder;

// Pull out argument list from the stack and build a function
template <
    typename C, typename X, typename Y,
    typename... Elems
>
struct function_builder<C, X, Y, stack<Elems...>>
{
    typedef typename replace_type<
        C, X, Y
    >::type type(Elems...);
};

// Specialization for function replacements
// Builds function with replaced return type, and converted
// argument list (recursion starts with empty stack)
template <
    typename C, typename X, typename Y,
    typename... Params
>
struct replace_type<C(Params...), X, Y>
{
    typedef typename function_builder<
        C, X, Y,
        typename list_converter<
            X, Y,
            stack<>,
            Params...
        >::type
    >::type type;
};

Please forgive me if there are some syntactical errors in the code above; it is quite a large file already and I tried to only extract relevant information.

  • 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-03T13:28:53+00:00Added an answer on June 3, 2026 at 1:28 pm
    template < typename ... A >
    struct stack { };
    
    template < typename Stack, typename T >
    struct push_front;
    
    template < typename T, typename ... A >
    struct push_front<stack<A...>,T> {
        typedef stack<T, A ... > type;
    };
    
    template < typename Ret, typename Args >
    struct build_fun;
    
    template < typename Ret, typename ... A >
    struct build_fun<Ret, stack<A...> > {
        typedef Ret(*fptr)(A...);
        typedef decltype(*static_cast<fptr>(0)) type;
    };
    
    template < typename Match, typename Rep, typename Target >
    struct replace_match { typedef Target type; };
    
    template < typename Match, typename Rep >
    struct replace_match<Match, Rep, Match> { typedef Rep type; };
    
    template < typename Match, typename Rep, typename ... Types >
    struct replace;
    
    template < typename Match, typename Rep, typename Head, typename ... Tail >
    struct replace<Match,Rep,Head,Tail...>
    {
        typedef typename replace_match<Match,Rep,Head>::type my_match;
    
        typedef typename replace<Match, Rep, Tail...>::type next_set;
    
        typedef typename push_front<next_set, my_match>::type type;
    };
    
    template < typename Match, typename Rep >
    struct replace<Match,Rep>
    {
        typedef stack<> type;
    };
    
    template < typename Sig, typename Match, typename Rep>
    struct replace_fun_args;
    
    template < typename R, typename Match, typename Rep, typename ... Args >
    struct replace_fun_args
    {
        typedef typename replace<Match, Rep, Args...>::type arg_stack;
        typedef typename build_fun<R,arg_stack>::type type;
    };
    
    #include <iostream>
    #include <typeinfo>
    
    int main() {
    
        replace<int,char,double,unsigned int, int, char*>::type t;
    
        std::cout << typeid(build_fun<void,decltype(t)>::type).name() << std::endl;
    }
    

    There’s probably a way to just use packs instead of the stack template…need to look up how to build a pack from types.

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

Sidebar

Related Questions

I am currently writing a system that stores meta data for around 140,000 ish
I have a working or mostly working POM that fetches all and exactly just
In a function that I am writing myself, I would like to invoke the
I've never been able to properly use the setTimeout function, so I tried writing
In my customized function I met a strange problem. I'm writing a function to
Writing documentation in html requires some code examples. What to do with characters that
Writing a client application that sends images to a server via a webservice. As
I writing a JSP program that needs to react on an existing program. It
I was just writing a generic object factory and using the boost preprocessor meta-library
In my class we are writing our own copy of C's malloc() function. To

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.