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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T01:31:19+00:00 2026-06-02T01:31:19+00:00

I have the following code: template <class Ret> class Foo { public: template <class

  • 0

I have the following code:

template <class Ret>
class Foo
{
public:
    template <class T>
    void foo(T&, const std::function<Ret()>&)
    {
        std::cout << "std::function<Ret()>\n";
    }
    template <class T>
    void foo(T&, Ret(T::*)() const)
    {
        std::cout << "Ret(T::*)() const\n";
    }
    template <class T>
    void foo(T&, Ret(T::*)())
    {
        std::cout << " Ret(T::*)()\n";
    }
};

class A
{
public:
    void foo1() const
    {
    }
    void foo()
    {
    }
};

 int main()
 {

     A a;
     Foo<void> f;
     f.foo(a, &A::foo);
     f.foo(a, &A::foo1);
     f.foo(a, std::bind(&A::foo, a));
 }

It works well, but I don’t want to have 2 different function for const and non-const member function pointer. So the question is: is there a way to merge
void foo(T&, const std::function<Ret()>&) and void foo(T&, Ret(T::*)() const) to one function? Note, that there is std::function overload which should also participate in resolution after merge. I need some function which will take member function pointers only. And all other will make its way to std::function version.

  • 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-02T01:31:21+00:00Added an answer on June 2, 2026 at 1:31 am

    You seem to be asking one implicit question and one explicit question. The answer to your implicit question, “how to merge the const and nonconst versions”, is as follows

    template<typename T, typename U, typename enable_if<is_fuction<T>::value, int>::type = 0> 
    void takesboth(T U::*);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: template <class T> static std::string ToString(const T& t) {
I have the following code, which does some iterator arithmetic: template<class Iterator> void Foo(Iterator
I have the following code: template <class T> struct pointer { operator pointer<const T>()
I have the following code in my Drupal 7 template.php file: function mytheme_preprocess_views_view__videos__videos(&$vars) {
I have the following code fragment: template <class T> struct ServicePtr { std::shared_ptr<T> service;
i have following code #include <iostream> #include <utility> using namespace std; namespace rel_ops{ template<class
I have following code: #include <iostream> using namespace std; template<class T> T max(T *data,int
I have the following code. template<class key,class val> bool has_key(key chkey,std::map<key,val> map){ for (std::map<key,val>::iterator
I have the following small code: template <typename T> class V { public: T
I have the following code: template<typename T, typename Allocator = std::allocator<T> > class Carray

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.