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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:12:42+00:00 2026-06-17T23:12:42+00:00

I have a specific question about adding supported types to existing legacy std::list manipulation

  • 0

I have a specific question about adding supported types to existing legacy std::list manipulation functions. I tried to do this with templates without success, so I’d appreciate any advice on how to use templates better or use different mechanisms entirely. Please take a look at my scenario below and let me know if there’s any thoughts on this. Thanks in advance for any help!

I currently have two std::lists of type A, and have insertion/removal/getter/etc functions for them. A simplified code snippet for this is shown below.

typedef std::list<A*> TListA;
TListA ListA1;
TListA ListA2;

void InsertIntoListA(A* pA)
{
    TListA& ListA = IsBoo()? ListA1 : ListA2;
    ListA.push_back(pA);
}

Now, it turns out I need to add type B, and I considered using templates to add this new type (as shown below), but it turns out there are two issues with that.

template <typename T>
void InsertIntoListT(T* pT)
{
    std::list<T*>& List;

    if (IsA())
        List = IsBoo()? ListA1 : ListA2;
    else
        List = IsBoo()? ListB1 : ListB2;      

    List.push_back(pT);   
}

Issue 1:
I cannot have “std::list& List” because since it’s by reference, it needs to be assigned to an actual list. So I would end up with something like this, which is not ideal.

template <typename T>
void InsertIntoListT(T* pT)
{    
    if (IsA()) {
        TListA& ListA = IsBoo()? ListA1 : ListA2;
        ListA.push_back(pT);   
    } else {
        TListB& ListB = IsBoo()? ListB1 : ListB2;      
        ListB.push_back(pT);   
    }     
}

Issue 2:
I am getting type conversion errors with either A to B, or B to A. I think this is because given the template T, the compiler will enumerate all four possibilities for “ListA.push_back” and “ListB.push_back,” which leads to inserting A to list A, inserting B to list A, inserting A to list B, and inserting B to list B, and only two of these are valid. So I end up with something like this, which I think defeats the purpose of using templates.

template <typename T>
void InsertIntoListT(T* pT)
{    
    if (IsA()) {
        TListA& ListA = IsBoo()? ListA1 : ListA2;
        ListA.push_back(reinterpret_cast<A*>(pT));
    } else {
        TListB& ListB = IsBoo()? ListB1 : ListB2;      
        ListB.push_back(reinterpret_cast<B*>(pT));
    }     
}
  • 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-17T23:12:44+00:00Added an answer on June 17, 2026 at 11:12 pm

    Don’t mix all in one function. It’s very bad practice. Use something like

    void InsertIntoListImpl(A* p)
    {
       (IsBoo() ? ListA1 : ListA2).push_back(p);
    }
    
    void InsertIntoListImpl(B* p)
    {
       (IsBoo() ? ListB1 : ListB2).push_back(p);
    }
    
    template<typename T>
    void InsertIntoList(T* p)
    {
       InsertIntoListImpl(p);
    }
    

    or some traits, or something else. But anyway, not many conditions in one function.

    Any why not pass actual list to function for insert? It will be better.

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

Sidebar

Related Questions

I have a quick question about encapsulating specific types with typedef . Say I
I have a rather specific question about how to exclude items of one list
I have a specific question about looping variable names in WPF. XAML code looks
I have a fairly specific question about concurrent programming in C. I have done
I have a question about whether or not a specific way of applying of
i have this specific question to do to you, i have a database from
This is quite a specific question, and I have had no luck on the
Today I have another specific question about a new feature in windows 7 called
Hello guys I have a specific question about the AND clause in SQL. The
I have a specific question about the http-protocol? I know that the http protocol

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.