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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:34:27+00:00 2026-05-27T12:34:27+00:00

Please, consider the code below: template<typename T> bool function1(T some_var) { return true; }

  • 0

Please, consider the code below:

template<typename T>
bool function1(T some_var) { return true; }

template <typename T>
bool (*function2())(T) {
  return function1<T>;
}

void function3( bool(*input_function)(char) ) {}

If I call

function3(function2<char>());

it is ok. But if I call

function3(function2());

compiler gives the error that it is not able to deduction the argument for template.

Could you, please, advise (give an idea) how to rewrite function1 and/or function2 (may be, fundamentally to rewrite using classes) to make it ok?

* Added *

I am trying to do something simple like lambda expressions in Boost.LambdaLib (may be, I am on a wrong way):

sort(some_vector.begin(), some_vector.end(), _1 < _2)

I did this:

template<typename T>
bool my_func_greater (const T& a, const T& b) {
  return a > b;
}

template<typename T>
bool my_func_lesser (const T& a, const T& b) {
  return b > a;
}

class my_comparing {
 public:
  int value;
  my_comparing(int value) : value(value) {}
  template <typename T>
  bool (*operator<(const my_comparing& another) const)(const T&, const T&) {
    if (this->value == 1 && another.value == 2) {
      return my_func_greater<T>;
    } else {
      return my_func_greater<T>;
    }
  }
};

const my_comparing& m_1 = my_comparing(1);
const my_comparing& m_2 = my_comparing(2);

It works:

sort(a, a + 5, m_1.operator< <int>(m_2));

But I want that it doesn’t require template argument as in LambdaLib.

  • 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-27T12:34:28+00:00Added an answer on May 27, 2026 at 12:34 pm

    Deduction from return type is not possible. So function2 can’t be deduced from what return type you expect.

    It is however possible to deduce cast operator. So you can replace function2 with a helper structure like: Unfortunately there is no standard syntax for declaring cast operator to function pointer without typedef and type deduction won’t work through typedef. Following definition works in some compilers (works in G++ 4.5, does not work in VC++ 9):

    struct function2 {
        template <typename T>
        (*operator bool())(T) {
            return function1<T>;
        }
    };
    

    (see also C++ Conversion operator for converting to function pointer).

    The call should than still look the same.

    Note: C++11 introduces alternative typedef syntax which can be templated. It would be like:

    struct function2 {
        template <typename T>
        using ftype = bool(*)(T);
    
        template <typename T>
        operator ftype<T>() {
            return function1<T>;
        }
    };
    

    but I have neither G++ 4.7 nor VC++ 10 at hand, so I can’t test whether it actually works.


    Ad Added:

    The trick in Boost.Lambda is that it does not return functions, but functors. And functors can be class templates. So you’d have:

    template<typename T>
    bool function1(T some_var) { return true; }
    
    class function2 {
        template <typename T>
        bool operator()(T t) {
            function1<T>;
        }
    };
    
    template <typename F>
    void function3( F input_function ) { ... input_function(something) ... }
    

    Now you can write:

    function3(function2);
    

    and it’s going to resolve the template inside function3. All STL takes functors as templates, so that’s going to work with all STL.

    However if don’t want to have function3 as a template, there is still a way. Unlike function pointer, the std::function (C++11 only, use boost::function for older compilers) template can be constructed from any functor (which includes plain function pointers). So given the above, you can write:

    void function3(std::function<bool ()(char)> input_function) { ... input_function(something) ... }
    

    and now you can still call:

    function3(function2());
    

    The point is that std::function has a template constructor that internally generates a template wrapper and stores a pointer to it’s method, which is than callable without further templates.

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

Sidebar

Related Questions

Please consider this code: #include <iostream> template<typename T> void f(T x) { std::cout <<
Please consider this code: template<typename T> char (&f(T[1]))[1]; template<typename T> char (&f(...))[2]; int main()
please consider the following code: template <typename T> struct foo { template <typename S>
A bit of help please, consider the bit of code below. public class Widget
please consider the code below, and tell me what I'm doing wrong. I want
Now please consider my below code of android layout <?xml version=1.0 encoding=utf-8?> <LinearLayout xmlns:android=http://schemas.android.com/apk/res/android
Please Consider my below xml code and screen shot(To view the large screen shotplease
Please consider this code snippet: private static void doSomething(Double avg, Double min, Double sd)
Please consider this code: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup
Please consider following code: 1. uint16 a = 0x0001; if(a < 0x0002) { //

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.