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

The Archive Base Latest Questions

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

I implemented a class filtered_ostream_iterator that helps to filter stream using predicate and I

  • 0

I implemented a class filtered_ostream_iterator that helps to filter stream using predicate and I made it as a template class.

template<typename T, typename Pred>
class filtered_ostream_iterator
{
    ostream& os;
    Pred _filter;

public:
    filtered_ostream_iterator(ostream & o, Pred filter): os(o),
                                                        _filter(filter) {}

    filtered_ostream_iterator& operator++()
    {
        return *this;
    }

    filtered_ostream_iterator& operator*()
    {
        return *this;
    }

    filtered_ostream_iterator& operator=(T t)
    {

        if (_filter(t))
            os << t;
        return *this;
    }
};

It’s ok but now I have a problem. When I use filtered_ostream_iterator I have to define it in following way.

stringstream ss1;
auto filter = [](char t){ return (t >= 'a' && t <=  'z') || (t >= 'A' && t <=  'Z'); };
filtered_ostream_iterator<char, bool (*)(char)> it1(ss1, filter); // initialization

It looks not really good especially <char, bool (*)(char)>. Then I decided to make a special function that can automatically inference types.

template<typename Pred>
filtered_ostream_iterator<char, Pred> create_filtered_ostream_iterator(ostream& os, Pred pred)
{
    return filtered_ostream_iterator<char, Pred>(os, pred); // problem
}

And I use it in following way

auto it1 = create_filtered_ostream_iterator(ss1, filter);

You can see that I should specify type of elements in stream and now it’s not a template actually, but when I’m trying to replace the code below with some thing like this

template<typename Pred, typename T>
filtered_ostream_iterator<T, Pred> create_filtered_ostream_iterator(ostream& os, Pred pred)
{
    return filtered_ostream_iterator<T, Pred>(os, pred); // error
}

And when I’m using it in the same way

auto it1 = create_filtered_ostream_iterator(ss1, filter);

I’m getting the following error.

error: no matching function for call to 'create_filtered_ostream_iterator'

So how should I avoid all these problems? Or should I use my first variant of definition and don’t mind how hard it looks? What do you think about it?

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

    Write it like this:

    template <typename T, typename Pred>
    filtered_ostream_iterator<T, Pred> create(std::ostream & os, Pred p)
    {
        return filtered_ostream_iterator<T, Pred>(os, p);
    }
    

    Usage:

    auto it = create<char>(ss1, filter);
    

    Only trailing template arguments can be deduced, but you’re free to specify as many initial arguments as you like.

    The alternative is to deduce the type from the stream:

    template <typename TChar, typename TTraits, typename Pred>
    filtered_ostream_iterator<typename TTraits::char_type, Pred>
    create(std::basic_ostream<TChar, TTraits> & os, Pred & p)
    {
        return filtered_ostream_iterator<typename TTraits::char_type, Pred>(os, p);
    }
    

    Usage:

    auto it = create(ss, filter);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an overloaded << operator from my reckful class implemented as follows: ostream&
I have a Static helper class implemented that helps cache and retreive some read-only,
I implemented class that serves as TcpCLient server. Looks like this: { [Export] public
I've implemented a Class that extends Overlay and also override the onTap / onTouchEvent
I have implemented a LoginAccess class that prompts the user to enter their active
I want to test my class MyTypeDAO implemented with Hibernate 4.1 using JUnit 4.9.
Interesting problem I ran into recently: I implemented a Stream class (a wrapping stream
I have a service class implemented in Java 6 / Spring 3 that needs
I have an IValueConverter implemented class and I need it to be injected using
I have a class implemented in C++ that's responsible for the arithmetic computation of

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.