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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:30:47+00:00 2026-05-28T00:30:47+00:00

There is my program that illustrates predicate template that could be used as sorting

  • 0

There is my program that illustrates predicate template that could be used as sorting predicate for STL containers instantiation:

#include <iostream>
#include <set>
#include <iterator>
#include <algorithm>
#include <functional>
#include <string>

using namespace std;

template<typename T, template <typename> class comp = std::less> struct ComparePtr: public binary_function<T const *, T const *, bool> {
  bool operator()(T const *lhs, T const *rhs) const {
    comp<T> cmp;
    return (cmp(*lhs, *rhs));
  }
};

int wmain() {
  string sa[] = {"Programmer", "Tester", "Manager", "Customer"};
  set<string *, ComparePtr<string>> ssp;
  for (int i(0) ; i < sizeof sa/sizeof sa[0] ; ++i)
    ssp.insert(sa + i);

  for_each(ssp.begin(), ssp.end(), [](string *s){ cout << s->c_str() << endl; });

  return 0;
}

Please, focus on predicate: is it written correctly?
Is it good to instantiate comp? Is there a way that allows not to instantiate comp predicate?

  • 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-28T00:30:48+00:00Added an answer on May 28, 2026 at 12:30 am

    Why use a template template at all and not just add a simple wrapper around general binary comparators?

    template<typename P>
    struct PointerPred {
      P p;
      template<typename T>
      bool operator()(const T& x, const T& y) { return p(*x, *y); }
    };
    

    Boost Pointer Containers could also make this a lot easier.

    Ildjarn showed how to implement your functor correctly:

    template<template<typename> class comp = std::less>
    struct ComparePtr {
      template<typename T>
      bool operator()(T const *lhs, T const *rhs) const {
        return comp<T>()(*lhs, *rhs);
      }
    };
    

    This way it isn’t necessary to specify the type of the arguments anymore.

    Edit: There used to be std::forward and rvalue references in my code, which made absolutely no sense.

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

Sidebar

Related Questions

Is there any API for writing a C# program that could interface with Windows
Is there a program that will print a nicely formatted tree from XML data?
I have a program, and in that program there is some variables (username and
I just want to know if there is a program that can convert an
Does there exist a free Windows software program that will help you generate regular
Is there a way to find the name of the program that is running
Is there any Visual Studio add-on (or windows/unix stand-alone program) that creates stub implementations
Is there a way using JNI and C# code to create a program that
I'm writing a program that contains a generational garbage collector. There are just two
I am now debugging a program that utilizes many different threads. There is 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.