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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T15:51:28+00:00 2026-05-21T15:51:28+00:00

I came across on a strange behavior of the following code, while playing around

  • 0

I came across on a strange behavior of the following code, while playing around with initialization of ints using g++ 4.4.3.

  int main()

    {

        int x(int());

        int y = int();

        cout << x << "  " << y <<  endl;
    }

the result is:

1 0

The value of “y” is 0 as expected, but the value of x is strangely “1”!

On VS2008 yields the following link error (a function declaration, but without definition):

unresolved external symbol "int __cdecl x(int (__cdecl*)(void))"

Can anyone explain this strange behavior of g++?

  • 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-21T15:51:29+00:00Added an answer on May 21, 2026 at 3:51 pm

    To complement GMan’s answer here (x is a function definition) as to why the 1.

    The reason for the output to be 1 is that at the place of call std::cout << x, the function decays into a pointer to the function (the language does not allow you to pass functions as arguments to other functions, so as with arrays an implicit conversion to pointer-to is performed). Now, there is no overload of an ostream that takes a function pointer, and the compiler tries to select a conversion to any of the available overloads. At that point it finds that the best conversion sequence is to bool and it prints 1 (the pointer is not 0).

    You can check this by changing the behavior, you can use std::cout << std::boolalpha << x, and that will print true instead of 1. Also, it is interesting to note that VS is right with this one, as the expression std::cout << x requires taking the address of x, then the function is used and the program is ill-formed if there is no definition for that function. You can again check that by providing a definition:

    int f() {}
    int main() {
       int x(int());      // 1
       x( &f );           // 2
    }
    int x( int(*)() ) {   // 3
       std::cout << "In x" << std::endl;
    }
    

    Where I have manually performed the conversion from function to pointer-to-function in the definition of x (1) and the call with the argument f (2) –note that the declaration in 1 and the definition in 3 are the same signature, and that the & in x( &f ) will be performed by the compiler if you don’t do it.

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

Sidebar

Related Questions

I came across a strange behavior while theming Drupal. I turned a few modules
I came across a rather strange problem with linq-to-sql. In the following example, var
I came across a very strange behavior in asp.net's ObjectDataSource , the description to
I'm developing my own Eclipse plugin and came across a very strange behavior. In
Came across something strange while migrating to my new server. I have a script
I have just started python and came across something kind of strange. The following
I came across this piece of code on a website. main(i) { gets(&i); puts();
While formulating an answer to another SO question , I came across some strange
Today I came across a very strange JavaScript regular expression issue. When using the
I came across a strange behaviour when doing some regular expressions in JavaScript today

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.