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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:10:13+00:00 2026-06-12T07:10:13+00:00

Say you write a really bad class template <typename T> class IntFoo { T

  • 0

Say you write a really bad class

template <typename T>
class IntFoo
{
  T container ;
public:
  void add( int val )
  {
    // made an assumption that
    // T will have a method ".push_front".
    container.push_front( val ) ;
  }
} ;

Ignore the fact that the class assumes the container will be something<int>, instead pay attention to the fact that

IntFoo< list<int> > listfoo ;
listfoo.add( 500 ) ; // works

IntFoo< vector<int> > intfoo;
//intfoo.add( 500 ) ; // breaks, _but only if this method is called_..

In general, is it ok to call a member function of a parameterized type like this? Is this bad design? Does this (anti)pattern have a name?

  • 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-12T07:10:14+00:00Added an answer on June 12, 2026 at 7:10 am

    This is perfectly fine and called compile-time duck typing and employed at all kinds of places all over the standard library itself. And seriously, how would you do anything useful with a template without assuming the template argument to support certain functionalities?

    Let’s take a look at any algorithm in the stdlib, e.g., std::copy:

    template<class InIt, class OutIt>
    OutIt copy(InIt first, Init last, OutIt out){
      for(; first != last; ++first)
        *out++ = *first;
      return out;
    }
    

    Here, an object of type InIt is assumed to support operator*() (for indirection) and operator++() for advancing the iterator. For an object of type OutIt, it’s assumed to support operator*() aswell, and operator++(int). A general assumption is also that whatever is returned from *out++ is assignable (aka convertible) from whatever *first yields. Another assumption would be that both InIt and OutIt are copy constructible.

    Another place this is used is in any standard container. In C++11, when you use std::vector<T>, T needs to be copy constructible if and only if you use any member function that requires a copy.

    All of this makes it possible for user-defined types to be treated the same as a built-in type, i.e., they’re fist-class citizens of the language. Let’s take a look at some algorithms again, namely ones that take a callback that is to be applied on a range:

    template<class InIt, class UnaryFunction>
    InIt for_each(InIt first, InIt last, UnaryFunction f){
      for(; first != last; ++first)
        f(*first);
      return first;
    }
    

    InIt is assumed to support the same operations again as in the copy example above. However, now we also have UnaryFunction. Objects of this type are assumed to support the post-fix function call notation, specifically with one argument (unary). Further is assumed that this the parameter of this function call is convertible from whatever *first yields.

    The typical example for the usage of this algorithm is with a plain function:

    void print(int i){ std::cout << i << " "; }
    
    int main(){
      std::vector<int> v(5); // 5 ints
      for(unsigned i=0; i < v.size(); ++i)
        v[i] = i;
      std::for_each(v.begin(), v.end(), print); // prints '0 1 2 3 4 '
    }
    

    However, you can also use function objects for this – a user-defined type that overloads operator():

    template<class T>
    struct generate_from{
      generate_from(T first) : _acc(first) {}
      T _acc;
      void operator()(T& val){ val = _acc++; }
    };
    
    int main(){
      std::vector<int> v(5); // 5 ints
      // yes, there is std::iota. shush, you.
      std::for_each(v.begin(), v.end(), generate_from<int>(0)); // fills 'v' with [0..4]
      std::for_each(v.begin(), v.end(), print); // prints '0 1 2 3 4 '
    }
    

    As you can see, my user-defined type generate_from can be treated exactly like a function, it can be called as if it was a function. Note that I make several assumptions on T in generate_from, namely it needs to be:

    • copy-constructible (in the ctor)
    • post-incrementable (in the operator())
    • copy-assignable (in the operator())
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

say I'm about to write an application with a thin GUI layer, a really
Let's say I have a program to write text into a file (not really
Say I want to write a function like this: int get_some_int(string index) { ...perform
If I write a serialversionUid for my class as say 1234, and I know
Personally I really prefer Unit Testing and write them for good coverage. (let's say
Let's say I have the following method: public static int CountNonNullMembers<T>(this IEnumerable<T> enumerable) {
Say I write this: from subprocessing import Popen, STDOUT, PIPE p = Popen([myproc], stderr=STDOUT,
Say I wanted to write a program that would export calendar data so that
Let's say I want to write a poker app for Android. It is fine
Say that a method only reads data from a database and does not write

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.