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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:48:30+00:00 2026-06-18T03:48:30+00:00

I have an abstract template class template <typename T> class Predicate1 { public: Predicate1();

  • 0

I have an abstract template class

template <typename T>
class Predicate1
{
  public:

    Predicate1();

    virtual ~Predicate1();

    virtual bool operator() (const T item) const = 0;
};

an implementation

class Pred : public Predicate1<string>
{
  public:

    virtual bool operator() (const string item) const;
};


bool Pred::operator()(const string item) const
{
  return item == "";
}

and a template class with a method filter that takes a predicate:

template <typename T>
class TList : public boost::enable_shared_from_this<TList<T> >
{
  public:

    typedef boost::shared_ptr<const TList<T> > List;

    const List filter(const Predicate1<T>& p) const;

    ...
};

I then use the filter as follows:

int main(int argc, char *argv[])
{
  const TList<string>::List l1 = ...;

  const TList<string>::List l2 = l1->filter(Pred());
}

which works OK.
However, I do not know how to replace the functor with an anonymous function.
I have tried:

const TList<string>::List l2 =
  l1->filter([] (const string item) -> bool { return item == ""; });

The anonymous function has, as far as I understand, the same signature as the
functor’s () operator, so it should work. Instead, I get the compiler error:

error: no matching function for call to ‘TList<std::basic_string<char> >::filter(main(int, char**)::<lambda(std::string)>) const’
note: candidate is: const TList<T>::List TList<T>::filter(const Predicate1<T>&) const [with T = std::basic_string<char>, TList<T>::List = boost::shared_ptr<const TList<std::basic_string<char> > >]

So, somehow the types seem to be incompatible, but I do not understand if I have overlooked something or what I am doing wrong. Or in other words, how do I have to declare the filter method so that it can (only) accept an anonymous function with signature

const string -> bool

or, in general, const T -> bool?

  • 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-18T03:48:32+00:00Added an answer on June 18, 2026 at 3:48 am

    Instead of writing your own interface classes, use std::function:

    const List filter(const std::function<bool (const T)>& p) const;
    

    std::function has constructors from lambdas and functors, so you can continue to use Pred.

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

Sidebar

Related Questions

I have an abstract template method: class abstract MyTemplate { public void something(Object obj)
I have the following abstract class: public abstract class TemplateBase { public abstract string
I have an abstract class called 'Template' defined as: [DataContract] public abstract class Template
I have abstract BaseController, which basically looks like below: public abstract class BaseController :
Suppose we have abstract class A (all examples in C#) public abstract class A
I have a template class with a private map member template <typename T> class
I have a non-template abstract base class which is used in order to be
I have a generic model: public abstract class MyModel<T> where T: MyBase, new() {
I have an abstract base class which contains a private nested implementation. visual c++
Suppose I have a class such as Value defined below. template <typename T> class

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.