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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:48:32+00:00 2026-05-26T04:48:32+00:00

Does the following result in well-defined behavior? That is, if you cast a non-vararg

  • 0

Does the following result in well-defined behavior? That is, if you cast a non-vararg function f as a vararg function g and call g with the arguments that f expects, does the behavior match that of calling f with those arguments?

class Base {};

class Derived1 : public Base {
public:
    int getInt1() {return 1;}
};

class Derived2 : public Base {
public:
    int getInt2() {return 2;}
};

typedef int (*vfunc)(...);

int foo (vfunc f) {
    Derived1 d1;
    Derived2 d2;
    return f(&d1, &d2);
}

int bar (Derived1 * p1, Derived2 * p2) {
    return p1->getInt1() + p2->getInt2();
}

int main (int argc, char ** argv) {
    return foo((vfunc)bar); // Is this program guaranteed to return 3?
}

UPDATE

Is there some way I can get the program to be well-defined, even if using proprietary keywords? Such as doing some stuff like __cdecl mentioned here:

http://msdn.microsoft.com/en-us/library/984x0h58%28v=vs.80%29.aspx

My end goal is to have a matcher function that tries matching on a list of X pointers. The matcher function takes in a predicate (not necessarily a function… might be a list) and takes in a function that it will pass the matched results to. The callback function passed to it takes the same argument types and arity as the predicate matched.

  • 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-26T04:48:32+00:00Added an answer on May 26, 2026 at 4:48 am

    No, the behavior is undefined, per C++11 5.2.11/6 (reinterpret_cast):

    The effect of calling a function through a pointer to a function type that is not the same as the type used in the definition of the function is undefined.

    The type of bar is int(Derived1*, Derived2*). The type of the function pointed to by f (the expression through which the call is made) is int(...). The two are not the same and therefore the behavior is undefined.

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

Sidebar

Related Questions

Well, it turns out that I got this function defined in my program code:
On Oracle 9i, why does the following produce the result 'abc' select 'abc '
why does the following js expression: test1 foo bar test2.replace(/foo.bar/, $') result in the
I'm getting crazy with this IE 7... ==> hhttp://neu.emergent-innovation.com/ Why does following function not
Looking at the following code, does theme[sprite].img get nested inside result[definition].data (as theme[sprite].img is
I'm using the following auto-complete plugin that uses Jquery Auto Complete. It works well
The following result sets work well with will_paginate: Members.all(:limit => 5).paginate(:page => params[:page]) Members.all(:conditions
Does anyone know how to make the following Matlab code approximate the exponential function
Why does following code snippet doesn't work? It doesn't gives any error, but also
Can any one tell me? what does following means in ruby program: obj =

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.