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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:00:24+00:00 2026-05-28T05:00:24+00:00

void f(int){} typedef void (*f_ptr)(int); struct Functor{ void operator()(int){} }; struct X{ operator f_ptr(){

  • 0
void f(int){}
typedef void (*f_ptr)(int);

struct Functor{
  void operator()(int){}
};

struct X{
  operator f_ptr(){ return f; }
};

struct Y{
  operator Functor(){ return Functor(); }
};

int main(){
  X x; Y y;
  x(5); // works ?!
  y(5); // doesn't ?!
}

Live example on Ideone. Output:

error: no match for call to ‘(Y) (int)’

Q1: Why is the call to x(5) allowed, even though X only defines a conversion to function pointer, and not operator()?

Q2: Conversely, why is the same thing not allowed, if we define a conversion to another functor?

  • 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-28T05:00:25+00:00Added an answer on May 28, 2026 at 5:00 am
    x(5); // works ?!
    

    This implicitly casts x to an f_ptr and calls that. C++11 standard:

    § 13.3.1.1.2 Call to object of class type [over.call.object]

    2) In addition, for each non-explicit conversion function declared in T of the form

    operator conversion-type-id ( ) attribute-specifier-seqopt cv-qualifier ;
    

    […where conversion-type-id denotes the type “pointer to function of (P1,...,Pn) returning R”…]


    y(5); // doesn't ?!
    

    The standard doesn’t mention anything about implicit conversion to class types that overload operator() (aka functors), which implies that the compiler doesn’t allow that.

    You must cast it explicitly:

    static_cast<Functor>(y)(5);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

void main() { typedef boost::ptr_map<int, char> MyMap; //typedef std::map<int, char *> MyMap; // in
I have a function pointer defined by: typedef void (*EventFunction)(int nEvent); Is there a
Consider this program: #include <map> #include <vector> typedef std::vector<int> IntVector; typedef std::map<IntVector,double> Map; void
int main(void) { int* p = (int*) malloc(sizeof(int)); int* q = (int*) malloc(sizeof(int)); *p
I got this C code. #include <stdio.h> int main(void) { int n, d, i;
#include <iostream> #include <vector> using namespace std; int main(void) { int i, s, g;
I have a struct like this: typedef struct { int sizes[3]; float **vals[3]; //
template<typename T> struct function { typedef T type; template<typename U> static void f() {}
This code compiles (as I would expect): typedef void __stdcall (*Func)(); struct A {
I have the following code: typedef void VOID; int f(void); int g(VOID); which compiles

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.