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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:12:26+00:00 2026-06-07T06:12:26+00:00

Context I have a custom comparator that takes another comparator and applies an additional

  • 0

Context

I have a custom comparator that takes another comparator and applies an additional check:

template <template <typename> class Comparator, typename T>
struct SoftOrder : public std::binary_function<T, T, bool> {
    bool operator()(const T lhs, const T rhs) const {
        return Comparator<T>()(lhs, rhs) && AnotherCheck();
    }
};

I have a second class that accepts a comparator, e.g.:

template <template <typename> class Comparator>
class Processor { ... };

It is easy to instantiate a Processor with a standard comparator (e.g. std::less) like so:

Processor<std::less> processor1;
Processor<std::greater> processor2;

However it is not so easy to instantiate with SoftOrder as the compiler correctly complains about the missing second template argument:

Processor<SoftOrder<std::less> > processor3; // <-- Fails to compile

Current Solutions

I have come up with a few solutions prior to posting this question.

First Solution – Lots of Derived Classes

template <typename T>
struct SoftOrderLessThan : public SoftOrder<std::less, T> {};

template <typename T>
struct SoftOrderGreaterThan : public SoftOrder<std::greater, T> {};

The main drawback of this solution is the need to create a new struct every time a new variant is required, e.g.:

template <typename T>
struct SoftOrderLessThan : public SoftOrder<std::less, T> {}; // Never used after the next line.
Processor<SoftOrderLessThan> processor3;

Second Solution – A very specific bind class

template <template <typename> class Comparator>
struct BindToSoftOrder {
    template <typename T>
    struct type : public SoftOrder<Comparator, T> {};
};

This is slightly better in that we don’t need to create the intermediate classes explicitly:

Processor<BindToSoftOrder<std::less>::type> processor3;

The downside is the requirement of a class specialised for this situation which cannot really be generalised by making SoftOrder a template parameter on BindToSoftOrder as this would make it a template<template<template>>> which is not permitted by the standard.

Third Solution – C++11 template aliases

template <typename T>
using SoftOrderLessThan = SoftOrder<std::less, T>;

Nicer than the first option in that it doesn’t require the introduction of new classes, however still requires littering the code with this extra code that is only used in passing onwards to another template class:

template <typename T>
using SoftOrderLessThan = SoftOrder<std::less, T>; // Never used again
Processor<SoftOrderLessThan> processor3;

Finally, the question

Is there a generic way to bind my custom comparator to a specific comparator in the following manner?

Processor<SomeCoolMetaTemplateBind<SoftOrder, std::less>::type> processor3;

I believe if all of the template parameters were simple types I could just do something like Processor<boost::mpl::bind<SoftOrder, std::less> >, but the presence of the template type in the template parameter list prevents this from occurring.

An ideal solution would involve C++03, but am happy to hear C++11 solutions as well.

If it’s not possible, I hope at least the question was interesting.

  • 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-07T06:12:27+00:00Added an answer on June 7, 2026 at 6:12 am

    Seems like this would work:

    template <
      template <template <typename> class,class> class U,
      template <typename> class X
    >
    struct SomeCoolMetaTemplateBind {
      template <typename T>
      struct type : public U<X,T> {
      };
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The Context I have a custom template container class put together from a map
Currently I have a custom view: public class MyView extends View { public MyView(Context
i have a custom layout called myWebView that is extended from WebView. class myApp{
I have a custom context: public class MyContext { public String doSomething() {...} }
I have a custom SurfaceView that looks like this: public class GFXSurface extends Activity
I have custom app class public class MyApp extends Application { public static Context
I have my own custom Content Provider that loads a database which contains the
I have module that implements custom content type via NodeAPI hooks ( hook_insert ,
I have a Button and the button content is binded to a custom class
We have a custom connection string provider that connects to different databases based on

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.