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

  • Home
  • SEARCH
  • 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 7178433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:55:14+00:00 2026-05-28T16:55:14+00:00

Consider the following class: class MyClass { public: template<class T> typename T::result_type apply(T& func)

  • 0

Consider the following class:

class MyClass
{
public:
  template<class T> typename T::result_type apply(T& func)
  {
    if (is_int())
    {
      return func(int(0));
    }
    return func(double(0));
  }
  ...
};

(The code does not look terribly useful, but it is only a contrived sample to demonstrate my point)

Anyway, a typical functor would be something like this:

struct MyFunc
{
  typedef void result_type;
  template<class V> void operator()(V)
  {
    // do something
  }
};

And one would use it like so:

MyClass c;
MyFunc f;
c.apply(f);

My question is this – can MyClass::apply be changed to recognize a slightly different version of the functor in addition to the original one, for instance the one expecting the caller object reference to be passed along with all the other parameters, something like this:

struct MyFuncEx
{
  typedef void result_type;
  template<class V> void operator()(const MyClass& caller, V)
  {
    // do something
  }
};

So, the following code would compile too:

MyClass c;
MyFunc f;
c.apply(f);
MyFuncEx f2;
c.apply(f2);

As a bonus I would like the compilation to fail if the functor contains both overloads, i.e. the following should fail the compilation:

struct MyFuncSmartAss
{
  typedef void result_type;
  template<class V> void operator()(V)
  {
    // do something
  }
  template<class V> void operator()(const MyClass& caller, V)
  {
    // do something
  }
};

...

MyClass c;
c.apply(MyFuncSmartAss());

But, it is not that important as long as the longer overload takes the precedence over the shorter one.

  • 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-28T16:55:15+00:00Added an answer on May 28, 2026 at 4:55 pm

    It really depends whether you have C++11 or not. This should solve the overload issue in C++11 (*):

    class MyClass {
    private:
      int _int;
      double _double;
    
    public:
      template <typename F>
      auto apply(F& f) -> decltype(f(_int), f(_double)) {
        if (is_int()) { return f(_int); }
        return f(_double);
      }
    
      template <typename F>
      auto apply(F& f) -> decltype(f(*this, _int), f(*this, _double)) {
        if (is_int()) { return f(*this, _double); }
        return f(_double)
      }
    
    };
    

    How does it work ?

    • Removing unsuitable overloads: the trailing-return-type specification with decltype creates an unevaluated context. The compiler performs regular overload resolution of the expression but only cares about the type. If an error occurs (the operator() does not exist in f), then we hit SFINAE and this overload of apply is discarded
    • Ambiguity if both work: if both overloads are suitable (because F provides both operators) then the call is ambiguous

    (*) I am not too sure about the correctness of the trailing-return-type specification, and more specifically the use of this and _arg. Both Clang 3.0 and gcc 4.5.2 error out. This can be worked around, just get a bit more verbose.

    // first
    decltype(f(0), f(0.0))
    
    // second
    decltype(f(std::declval<MyClass>(), 0), f(std::declval<MyClass>(), 0.0))
    

    In action at ideone with this workaround:

    • No possible function.
    • Suitable overload is selected.
    • Ambiguous calls avoided.

    EDIT: to cope with the int/double requirement.

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

Sidebar

Related Questions

Consider the following code: class MyClass { template <typename Datatype> friend MyClass& operator<<(MyClass& MyClassReference,
Consider the following template class class MyClassInterface { public: virtual double foo(double) = 0;
Consider the following code: public class MyClass { public static string MyStaticMethod() { //string
Consider following class class test { public: test(int x){ cout<< test \n; } };
Consider the following class: class Something : ISomething { public void DoesSomething(int x) {
Consider the following class public class Class1 { public int A { get; set;
Consider the following code: class A { public: virtual void f() throw ( int
Consider the following code: public class MyClass { public delegate string PrintHelloType(string greeting); public
Consider the following proxy class: class VertexProxy { public: VertexProxy(double* x, double* y, double*
Consider the following class in a file MyClass.cs using System; public class MyClass :

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.