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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:42:41+00:00 2026-06-08T17:42:41+00:00

I need to pass a function to an operator. Any unary function having correct

  • 0

I need to pass a function to an operator. Any unary function having correct arg type. Return type can be anything. Because this is library code, I can not wrap it or cast f to specific overload (outside of operator*). Function takes operator* 1st arg as it own argument. Artificial example below compiles and returns correct results. But it has hardcoded int return type—to make this example compile.

#include <tuple>
#include <iostream>
using namespace std;

template<typename T>
int operator* (T x,  int& (*f)(T&) ) {
    return (*f)(x);
};

int main() {
    tuple<int,int>  tpl(42,43);
    cout << tpl * get<0>;
}

Is it possible to make operator* to accept f with arbitrary return type?

UPDATE – GCC bug?
Code:

#include <tuple>

template<typename T, typename U> 
U operator* (T x,  U& (*f)(T&) ) {  
    return (*f)(x);
}; 

int main() {
    std::tuple<int,int>  tpl(42,43);
    return   tpl * std::get<0,int,int>;
}  

Compiles and runs correctly with gcc462 and 453 but is reject with gcc471 and 480. So it is possible GCC regression bug. I’ve submitted bug report:
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54111

EDIT
I’ve changed example to use tuple as arg – it was possible trivially deduce return type in previous example.

EDIT2
Many people could not understand what is needed, so I’ve changed call function to operator* to make example more real.

  • 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-08T17:42:43+00:00Added an answer on June 8, 2026 at 5:42 pm

    As an answer to your updated question:

    as discussed by @DavidRodríguez, get<0> is not enough, nor the syntatically correct &get<0>. What you need is &get<0,int,int>. Follows your example, it would be:

    #include <tuple>
    using namespace std;
    
    template<typename T, typename U>
    U call (T x, U (*f)(T&) ) {
          return (*f)(x);
    };
    
    int main() {
        tuple<int,int>  tpl(42,43);
        call(tpl, &get<0,int,int>);
        return 0;
    }
    

    During normal use of std::get<>(), the int,int part is deduced automatically. But in your situation you need to provide it, since there is no parameters. One workaround is a custom get template function:

    #include <tuple>
    using namespace std;
    
    template <size_t I, typename T>
    auto myGet(T& tpl) -> decltype(get<I>(tpl))
    {
        return get<I>(tpl);
    }
    
    template<typename T, typename U>
    U call (T x, U (*f)(T&) ) {
          return (*f)(x);
    };
    
    
    int main() {
        tuple<int,int>  tpl(42,43);
        auto get0 = &myGet<0, decltype(tpl)>;
        call(tpl, get0);
    
    //  call(tpl, &myGet<0, decltype(tpl)>); // all in one line, do not work
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi What paratemeter I need to pass to this function. It is taking 00c0c0c0
This question is regarding getopt function in php. I need to pass two parameter
Can I pass additional parameters to a predicate function? I need it in a
How can I directly pass a value from the diamond operator to a function
I need to pass class function as argument for glutIdleFunc. This construction doesn't work:
I need to pass this function as a callback to DOM event var fooo
I have a pretty large object that I need to pass to a function
I need to pass multiple arguments to a function that I would like to
I need to pass the $route to its inner function,but failed: function compilePath( $route
I need to pass a string literal to a function myfunction(arg1 DEF_CHAR arg1); now

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.