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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:45:45+00:00 2026-05-26T04:45:45+00:00

Okay, simple template question. Say I define my template class something like this: template<typename

  • 0

Okay, simple template question. Say I define my template class something like this:

template<typename T>
class foo {
public:
    foo(T const& first, T const& second) : first(first), second(second) {}

    template<typename C>
    void bar(C& container, T const& baz) {
        //...
    }
private:
    T first;
    T second;
}

The question is about my bar function… I need it to be able to use a standard container of some sort, which is why I included the template/typename C part, to define that container type. But apparently that’s not the right way to do it, since my test class then complains that:

error: ‘bar’ was not declared in this scope

So how would I go about implementing my bar function the proper way? That is, as a function of my template class, with an arbitrary container type… the rest of my template class works fine (has other functions that don’t result in an error), it’s just that one function that’s problematic.

EDIT:
Okay, so the specific function (bar) is an eraseInRange function, that erases all elements in a specified range:

void eraseInRange(C& container, T const& firstElement, T const& secondElement) {...}

And an example of how it would be used would be:

eraseInRange(v, 7, 19);

where v is a vector in this case.

EDIT 2:
Silly me! I was supposed to declare the function outside of my class, not in it… pretty frustrating mistake to be making. Anyways, thanks everyone for the help, though the problem was a little different, the information did help me construct the function, since after finding my original problem, I did get some other pleasant errors. So thank you!

  • 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-26T04:45:46+00:00Added an answer on May 26, 2026 at 4:45 am

    Traits solution.

    Generalize not more than needed, and not less.

    In some cases that solution might not be enough as it will match any template with such signature (e.g. shared_ptr), in which case you could make use of type_traits, very much like duck-typing (templates are duck typed in general).

    #include <type_traits>
    
    // Helper to determine whether there's a const_iterator for T.
    template<typename T>
    struct has_const_iterator
    {
    private:
        template<typename C> static char test(typename C::const_iterator*);
        template<typename C> static int  test(...);
    public:
        enum { value = sizeof(test<T>(0)) == sizeof(char) };
    };
    
    
    // bar() is defined for Containers that define const_iterator as well
    // as value_type.
    template <typename Container>
    typename std::enable_if<has_const_iterator<Container>::value,
                            void>::type
    bar(const Container &c, typename Container::value_type const & t)
    {
      // Note: no extra check needed for value_type, the check comes for
      //       free in the function signature already.
    }
    
    
    template <typename T>
    class DoesNotHaveConstIterator {};
    
    #include <vector>
    int main () {
        std::vector<float> c;
        bar (c, 1.2f);
    
        DoesNotHaveConstIterator<float> b;
        bar (b, 1.2f); // correctly fails to compile
    }
    

    A good template usually does not artificially restrict the kind of types for which they are valid (why should they?). But imagine in the example above you need to have access to an objects const_iterator, then you can use SFINAE and type_traits to put those constraints on your function.

    Or just to as the standard library does

    Generalize not more than needed, and not less.

    template <typename Iter>
    void bar (Iter it, Iter end) {
        for (; it!=end; ++it) { /*...*/ }
    }
    
    #include <vector>
    int main () {
        std::vector<float> c;
        bar (c.begin(), c.end());
    }
    

    For more such examples, look into <algorithm>.

    This approach’s strength is its simplicity and is based on concepts like ForwardIterator. It will even work for arrays. If you want to report errors right in the signature, you can combine it with traits.

    std containers with signature like std::vector (not recommended)

    The simplest solution is approximated by Kerrek SB already, though it is invalid C++. The corrected variant goes like so:

    #include <memory> // for std::allocator
    template <template <typename, typename> class Container, 
              typename Value,
              typename Allocator=std::allocator<Value> >
    void bar(const Container<Value, Allocator> & c, const Value & t)
    {
      //
    }
    

    However: this will only work for containers that have exactly two template type arguments, so will fail miserably for std::map (thanks Luc Danton).

    Any kind of secondary template arguments (not recommended)

    The corrected version for any secondary parameter count is as follows:

    #include <memory> // for std::allocator<>
    
    template <template <typename, typename...> class Container, 
              typename Value,
              typename... AddParams >
    void bar(const Container<Value, AddParams...> & c, const Value & t)
    {
      //
    }
    
    template <typename T>
    class OneParameterVector {};
    
    #include <vector>
    int main () {
        OneParameterVector<float> b;
        bar (b, 1.2f);
        std::vector<float> c;
        bar (c, 1.2f);
    }
    

    However: this will still fail for non-template containers (thanks Luc Danton).

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

Sidebar

Related Questions

Okay, this is something that should be a simple matrix question, but my understanding
Okay. Complex Title for a simple(?) problem. I have something like this going on
Okay. Complex Title for a simple(?) problem. I have something like this going on
Okay, this is a simple question, but I'd like some oppinions on the correct
Okay this may be a simple question but I have yet to come with
Okay, this is following on from my previous question reguarding performing a simple ajax
Okay, I'm probably missing something really simple here, but I've been at this for
Okay, this seems like it should be relatively simple, but I've been Googling for
Okay, I'd like to write a simple C app for Linux (say Ubuntu with
Okay, I feel like I am missing something really simple here. I have an

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.