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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:26:26+00:00 2026-06-03T08:26:26+00:00

I am new to metafunctions. I want to write a function that replaces all

  • 0

I am new to metafunctions. I want to write a function that replaces all matches of a certain type in a compound type with some other type. In example: replace<void *, void, int>::type should be int *, replace<void, void, int>::type should be int, etc.

I basically failed with two different approaches so far:

template
    <
        typename C, // Type to be searched
        typename X, // "Needle" that is searched for
        typename Y  // Replacing type
    >
struct replace
{
    typedef C type;
};

// If the type matches the search exactly, replace
template
    <
        typename C,
        typename Y
    >
struct replace<C, C, Y>
{
    typedef Y type;
};

// If the type is a pointer, strip it and call recursively
template
    <
        typename C,
        typename X,
        typename Y
    >
struct replace<C *, X, Y>
{
    typedef typename replace<C, X, Y>::type * type;
};

This seemed pretty straight forward to me, but I discovered that when I try replace<void *, void *, int>, the compiler cannot decide whether to use replace<C, C, Y> or replace<C *, X, Y> in that case, so compilation fails.

The next thing I tried is to strip pointers in the base function already:

template
    <
        typename C,
        typename X,
        typename Y
    >
struct replace
{
    typedef typename boost::conditional
        <
            boost::is_pointer<C>::value,
            typename replace
                <
                    typename boost::remove_pointer<C>::type,
                    X, Y
                >::type *,
            C
        >::type
    type;
};

…and this is when I found out that I cannot do that either, because type is apparently not defined at that point, so I cannot do recursive typedef from the base function.

Now I am out of ideas. How would you solve such a problem?

  • 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-03T08:26:28+00:00Added an answer on June 3, 2026 at 8:26 am

    Here’s a general idea:

    template <typename, typename> struct pattern;
    
    template <typename T> struct pattern<T, T>
    {
        template <typename U> struct rebind
        {
            typedef U other;
        };
    };
    
    template <typename A, typename B> struct pattern<A*, B>
    {
        template <typename U> struct rebind
        {
            typedef typename pattern<A, B>::template rebind<U>::other * other;
        };
    };
    
    template <typename Haystack, typename Needle, typename New>
    struct replace
    {
        typedef typename pattern<Haystack, Needle>::template rebind<New>::other type;
    };
    

    Test:

    #include <demangle.hpp>
    #include <iostream>
    int main()
    {
        typedef replace<void, void, int>::type T1;
        typedef replace<void*, void, int>::type T2;
    
        std::cout << demangle<T1>() << std::endl;
        std::cout << demangle<T2>() << std::endl;
    }
    

    Prints:

    int
    int*
    

    Edit: Here’s a somewhat more complete set:

    template <typename, typename> struct pattern;
    template <typename, typename> struct pattern_aux;
    
    template <typename A, typename B> struct pattern_aux
    {
        template <typename U> struct rebind
        {
            typedef typename pattern<A, B>::template rebind<U>::other other;
        };
    };
    
    template <typename A, typename B, unsigned int N> struct pattern_aux<A[N], B>
    {
        template <typename U> struct rebind
        {
            typedef typename pattern<A, B>::template rebind<U>::other other[N];
        };
    };
    
    
    template <typename A, typename B> struct pattern
    {
        template <typename U> struct rebind
        {
            typedef typename pattern_aux<A, B>::template rebind<U>::other * other;
        };
    };
    
    template <typename T> struct pattern<T, T>
    {
        template <typename U> struct rebind
        {
            typedef U other;
        };
    };
    
    template <typename A, typename B> struct pattern<A*, B>
    {
        template <typename U> struct rebind
        {
            typedef typename pattern<A, B>::template rebind<U>::other * other;
        };
    };
    
    template <typename A, typename B> struct pattern<A const, B>
    {
        template <typename U> struct rebind
        {
            typedef typename pattern_aux<A, B>::template rebind<U>::other const other;
        };
    };
    
    template <typename A, typename B> struct pattern<A volatile, B>
    {
        template <typename U> struct rebind
        {
            typedef typename pattern_aux<A, B>::template rebind<U>::other volatile other;
        };
    };
    
    template <typename A, typename B> struct pattern<A const volatile, B>
    {
        template <typename U> struct rebind
        {
            typedef typename pattern_aux<A, B>::template rebind<U>::other const volatile other;
        };
    };
    
    template <typename Haystack, typename Needle, typename New>
    struct replace
    {
        typedef typename pattern<Haystack, Needle>::template rebind<New>::other type;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

New to Rails, and following a tutorial that starts by creating the classic blog
New to webdev. I need to use the same menu on all the pages
New to flash. When playing with a dummy 'tutorial' app, we saw that when
New with jQuery. Here's what I have so far - basically, I want jQuery+ajax
New to objective-c, -(void) myfunction : (int) d It means that returning 'void' and
New to SQL and have a query pulling some data with a number of
New to OOP, eager to learn good habits. I want to make a vectorMap
New to Rails... here goes: If I want my 'create' method to respond differently
New to XML. I have a 3rd party webservice that supplies an XML document
New to ASP and probably never named a Javascript file .inc :-) But that

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.