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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:57:47+00:00 2026-06-01T20:57:47+00:00

I have a function declared as follows; its exact working is not relevant for

  • 0

I have a function declared as follows; its exact working is not relevant for this.

template<typename T>
std::pair<int, int>
partition3(T *pT, const int N, const T &Kq, const int w,
           std::function<int(const T&, const T&, int)> P);

At the call site I attempt to do the following:

bool partition3_test()
{
  struct cmp
  {
    int operator()(int x, int y, int) const
    { return x-y; }
  };

  int V1[11] = { 3, 7, 1, 7, 7, 8, 10, 2, 16, 4, 3 },
      V2[11] = { 3, 6, 1, 6, 6, 8, 10, 2, 16, 4, 3 };

  std::function<int(const int&, const int&, int)> F = cmp();

  std::pair<int, int>
    p1  = partition3(V1, 11, 7, 0, cmp()),
    p2  = partition3(V2, 11, 7, 0, cmp());

  return false;
}

For the two calls of partition3 the compiler (MSVC 2010) complains that it could not deduce template argument for the last parameter. If I replace cmp() with F, the code compiles and works fine.

I have two questions:

  1. Why do I get the error? [compiler bug or some arcane C++ rule?]
  2. How can I achieve the same effect without first explicitly constructing F?

(Currently, I have solved the problem by introducing another template parameter on partition3 and declaring P as that template type.)

  • 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-01T20:57:49+00:00Added an answer on June 1, 2026 at 8:57 pm

    cmp() isn’t actually a std::function at all. The fact that the copy-initialization works may be confusing the issue, but that uses a converting constructor which must be using some sort of wrapper object, and I’m surprised that it works against a temporary functor object (ah, checking the standard it apparently make a copy of the functor).

    On top of that, the function arguments don’t match (pass-by-value and pass-by-const-reference are source-compatible, but not runtime-call-compatible), which again requires an adaptor.

    The best solution is to make the template function more generic, so that it also works with raw function pointers and arbitrary functor objects, not just std::function:

    template<typename T, typename Functor>
    std::pair<int, int> partition3(T *pT, const int N, const T &Kq, const int w,
                                   const Functor& P);
    

    If for some reason you really wanted to use std::function (you need the virtual dispatch, for instance), you can. The error, quite contrary to what Nawaz wrote, is that this is a deducible context, but more than one type fits (this is where the adaptor/wrapper nature of std::function becomes important, because the type doesn’t have to exactly match the parameter, it just has to be compatible. std::function<int(const long&, const long&, int)> would match just as well.)

    Use instead a non-deducible context, that way the compiler won’t even try to use the functor for deduction of T.

    template<typename T, typename Functor>
    std::pair<int, int> partition3(T *pT, const int N, const T &Kq, const int w,
                                   std::function<std::identity<const T>::type&, std::identity<const T>::type&, int> P);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function that takes, amongst others, a parameter declared as int privateCount
I have written my own function, which in C would be declared like this,
I have a dropdownlist which is declared as follows: <select onchange=reloadValues(this,event);> ... options here
I have declared QOBJECT macro but still its calling the function without passing the
I have a function as follows: -(int)ladderCalc:(NSArray*)amounts percentages:(NSArray*)percentages amount:(int)amount { // Do some stuff
I have a function X() declared as PURE VIRTUAL in base class: class Base
I have a simple function in which an array is declared with size depending
I have declared an external function with a GCC weak attribute in a .c
Suppose I have a static variable declared inside a function in C. If I
I have a simple C function which I declare as: int strLen(const char* str_)

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.