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

  • Home
  • SEARCH
  • 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 8228943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:37:53+00:00 2026-06-07T16:37:53+00:00

in my work we do a lot of pair programming, and i wrote a

  • 0

in my work we do a lot of pair programming, and i wrote a function that ONLY accepts containers of a SINGLE type or its derivates, but my co-worker is afraid it will fail code review because it looks so damn ugly and says there’s gotta be a better way:

here is the signature, the Fruit class is a base class that i renamed Fruit just for this thread:

template <class Container>
typename enable_if<is_base_of<Fruit, typename remove_pointer<typename Container::value_type>::type>::value, void>::type
processFruits(container& fruits)
{
    //process the elements and change them if needed. thats why no const
}

what it does is: returns void and enables the function IF its a container AND the type inside the container is a Fruit and/or derivided of fruit. i also used std::remove_pointer because i needed to know the “type” of the pointer (the container will most likely have pointers).

this compiles and works as intended, but as i said i don’t know it its the best way to do it, it seems too verbose and might get cut of on code review.

EDIT: this also accepts templated classes, don’t have to be containers. is there a way i can limit it to only accept STL containers?

any alternate ideas or is it fine like it is?
thanks in advance.

  • 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-07T16:37:55+00:00Added an answer on June 7, 2026 at 4:37 pm

    It is a bit horrible to read.

    Well for starters you don’t need to say enable_if<B, void> you can just say enable_if<B> and use the default template argument.

    You can easily split it into separate pieces:

    template <class T>
      struct is_fruity
      : is_base_of<Fruit, T>
      { };
    
    template <class Container, typename Value = typename Container::value_type>
      struct is_fruit_container
      : is_fruity<typename remove_pointer<Value>::type>>
      { };
    
    template<class Container>
      typename enable_if<is_fruit_container<Container>::value>::type
      processFruits(Container& fruits)
      {
        //process the elements and change them if needed. thats why no const
      }
    

    If you have a compiler supporting alias templates you can make it even easier to read:

    template<typename Cond>
      using Require = typename enable_if<Cond::value>::type;
    
    template<class Container>
      Require<is_fruit_container<Container>>
      processFruits(Container& fruits)
      {
        //process the elements and change them if needed. thats why no const
      }
    

    this also accepts templated classes, don’t have to be containers. is there a way i can limit it to only accept STL containers?

    I’m not sure what you mean by “templated classes”, it only accepts types with a nested value_type type which is a type derived from Fruit or a pointer to such type, it doesn’t have to be a template. To limit it to “STL containers” you need to write a trait to indentify an “STL container”, however you want to define that. To do it properly you’d need a trait that tests for begin(), end(), size() members, as well as all the nested types specified by the container requirements, iterator, value_type, etc.

    template<typename C, typename Chead, typename... Ctail>
    struct static_and
    {
      static const bool value = C::value && static_and<Chead, Ctail...>::value;
    };
    
    template<typename C>
    struct static_and<C>
    {
      static const bool value = C::value;
    };
    
    template<typename C>
    struct is_container
    : static_and<has_begin<C>, has_end<C>, has_iterator<C>, has_value_type<C> /* etc */>
    { };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an MVC app that does a lot of work with jQuery ajax
I used to work in JavaScript a lot and one thing that really bothered
I have tried a lot of solutions but it did not work for me
Currently i work a lot with different type of scheduling-algorithms and i'm always in
I do a lot of work with GWT but don't have experience of Flex.
I work a lot with pairs of values: std::pair<int, int> my_pair . Sometimes I
I have an application that does a lot work on S3, mostly downloading files
Quick bash/terminal question - I work a lot on the command line, but have
I work a lot in PHP but I never really understand the namespace method
We work with a lot of real estate, and while rearchitecting how the data

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.