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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:12:45+00:00 2026-06-09T20:12:45+00:00

I would like to write a template function which accepts 2 values and a

  • 0

I would like to write a template function which accepts 2 values and a functor or a lambda.
The function calls the functor with those values and returns the result.

template <typename T, typename Fn> 
_ReturnTypeOfPred_ Apply(T x, T y, Fn fn)
  {
  return fn(x, y);
  }

Question: How can I define the return type of Apply to become equal to the return type of Fn? It is not necessarily equal to T, as in this example of the functor

template <typename T> 
auto Sum(T x, T y) -> decltype(x+y)
  {
  return x+y;
  }

Update

The 1st example was oversimplified.
Should this one work?

template <typename TContainer, typename Fn> 
auto Apply(const TContainer& x, const TContainer& y, Fn fn) -> decltype(fn(x.front(), y.front()))
  {
  return fn(x.front(), y.front());
  }

Would it always work if I repeat return expression in decltype of the return type? Is there a more elegant way?

  • 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-09T20:12:46+00:00Added an answer on June 9, 2026 at 8:12 pm

    You’re nearly there; just use decltype:

    template <typename T, typename Fn> 
    auto Apply(T x, T y, Fn fn) -> decltype(fn(x, y))
    {
      return fn(x, y);
    }
    

    You could use std::result_of (Difference between std::result_of and decltype) but why bother?

    template <typename T, typename Fn> 
    typename std::result_of<Fn, T, T>::type Apply(T x, T y, Fn fn)
    {
      return fn(x, y);
    }
    

    Regarding the follow-up question: for a function

    auto fn(<args>) -> <return-type> { return <expression>; }
    

    substituting return-type with decltype(<expression>) will usually work, but can be error prone. For example, consider:

    auto f(char c) -> decltype(std::string() += c) { return std::string() += c; }
    

    Here decltype will yield std::string & and your function will return an lvalue reference to a local! This would have to be changed to:

    auto f(char c) -> std::remove_reference<decltype(std::string() += c)>::type {
        return std::string() += c;
    }
    

    In other cases, <expression> could yield a value that is not returnable for reason of being e.g. noncopyable, containing a lambda, etc.

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

Sidebar

Related Questions

I would like to write a cross-platform function in C++ that contains system calls.
I'd like to write a function template like that: template< typename L<T> > //
I would like to write a function that creates a plot of a set
I would like to write a query that returns a single date value, that
Is it possible to write a c++ template function which takes a variable number
I would like to write some class which maps int to something using templates.
I would like to write an abstract base class class func_double_double_t : public unary_function<double,
I would like to write an if statement that would continue to repeat a
I would like to write a League Fixture generator in python, but I can't.
I would like to write to functional test for one of my rails 3

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.