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

The Archive Base Latest Questions

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

I just realize how hard asking questions can be… Hopefully I can give examples

  • 0

I just realize how hard asking questions can be… Hopefully I can give examples that are both, sufficiently precise to demonstrate my problem and short enough not to mess everything up… At least there’s the possibility to edit.

So this is kind of my situation at the moment. Of course I altered it a bit in terms of logic / structure (and in terms of naming anyway) trying to focus on the essence of my question:

// MyClass deals with lists (actually several data structures) of the
// type MyType which should support different types and has to be 
// efficiently dealt with. Templating seems just right here
class MyClass
{
  ...

  void doSomething<class MyType>(vector<MyType> someList);

  ...

  // At some point I have to extract elements of the type MyType.
  // The extractor obviously depends on MyType but it is not possible to
  // Create a general version that could use templates itself 
  // (unless I use a specialization for each possible MyType)

  // I am stuck between these two alternatives:

  // Possibility1:
  // Let the client pass the right extractor and template it.
  template<class Extractor, class MyType>
  void extract(const Extractor& extractor, const string& source, 
               vector<MyType>* dest)
  {
     extractor.extract(source, dest);
  }

  // Possibility2:
  // Use a member _extractor of some base type that has to be set
  // to a specialization. The ExtractorBase has a virtual method
  // template<T> void extract(const string& source, vector<T>* myType) = 0
  // with no definition that is only defined in subclasses wrt certain
  // postings.
  ExtractorBase _extractor;

  template<class MyType>
  void extract(const string& source, vector<MyType>* dest)
  {
     _extractor.extract(source, dest);
  }
}

At the moment I would prefer possibility1, because I don’t have to mess with inheritance in Extractor for all the variants of MyType and associated Extractor I want to try out in the future.

On the other hand extractors may require complex code and several members (something like huge maps that map certain inputs on certain values). So there will be no performance gain by using templates. Particularly using header file only Extractors and probably even functors that are supposed to get inlined, are kind of out of the question. In the past, this has been a strong pointer to me that templating will only increase code complexity (having to deal with instantiation, making life harder for client code, etc.) and that I should try to avoid it altogether.

Or is there a third possibility I didn’t think of at all?

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

    It’s better to go with the first option. It’s cleaner and maintainable.

    Because from your comments, I am making out that you are making a wrong assumption for the 2nd option:

    // The ExtractorBase has a virtual method
    // template<T> void extract(const string& source, vector<T>* myType) = 0;
    

    NO. That’s not possible; a template function can never be virtual. So to implement the 2nd option you have to opt for some dirty and hard to maintain ways, which is not a good idea.

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

Sidebar

Related Questions

I just can't realize why my read time out is not working. All I
It just makes me go mad. I can't realize what the problem is. Please
I realize that in most cases, it's preferred in Python to just access attributes
I realize that without code this might be hard to answer but the problem
I just realized that this piece of code works well in Firefox but not
I just realized that the MySQLWorkBench destroys the target (makes a replacement) when performing
I just realized that I need to synchronize a significant amount of data collection
I just realized that the method Element.getElementsByTagName(someTagName) returns a nodelist of all elements in
I just realized that i may not be following best practices in regards to
I just realized that when i start a task from within a task and

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.