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

The Archive Base Latest Questions

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

I guess I’ve found a G++ bug but I’m not sure. I cannot explain

  • 0

I guess I’ve found a G++ bug but I’m not sure. I cannot explain it. The compile shouldn’t pass BAD code but it does. g++-4.5 and g++4.6 -std=c++0x pass this code without any warning.

As is the compile thinks that pointer to Bar object is Bar object itself.
I’m crazy. I spent many hours to get the bug. Is there any technique to protect from this kind of bug?

Bad code gives:

 g++-4.6 for_stackoverflow.cpp && ./a.out
 address of bar in main()   0xbff18fc0
 Foo 0x9e80008      Bar     0xbff18fec
 Foo 0x9e80028      Bar     0xbff18fec
 Foo 0x9e80048      Bar     0xbff18fec
 end

Source code:

     #include <iostream>
     #include <list>
     #include <iomanip>
     #include <algorithm>

     #define BAD

     using namespace std;

     class Bar;

     class Foo {
     public:
       virtual void tick(Bar & b) {
         cout << "Foo " << this << "      Bar " << setw(14) << (&b) << endl;
       }    
     };

     class Bar : public list<Foo*> {
     };

     int main() {
       Bar bar;
       cout << "address of bar in main()   " << &bar << endl;
       bar.push_back(new Foo());
       bar.push_back(new Foo());
       bar.push_back(new Foo());
     #ifdef GOOD
       for_each(bar.begin(), bar.end(), bind2nd(mem_fun(&Foo::tick), bar));
     #elif defined(BAD)
       for_each(bar.begin(), bar.end(), bind2nd(mem_fun(&Foo::tick), &bar));
     #else
     #error "define GOOD xor BAD"
     #endif
       cout << "end" << endl;
       return 0;
     }
  • 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-28T03:21:41+00:00Added an answer on May 28, 2026 at 3:21 am

    bind2nd is declared as:

    template <class Fn, class T>
    binder2nd<Fn> bind2nd(const Fn&, const T&);
    

    This means that the type T is deduced, in this case as Bar *.

    On my system it’s implemented as:

    template<typename _Operation, typename _Tp>
    inline binder2nd<_Operation>
    bind2nd(const _Operation& __fn, const _Tp& __x)
    {
      typedef typename _Operation::second_argument_type _Arg2_type;
      return binder2nd<_Operation>(__fn, _Arg2_type(__x));
    } 
    

    To see why that would compile consider:

    class Bar {};
    
    int main() {
      Bar *b = 0;
      typedef const Bar& type;
      const type t = type(b);
    }
    

    which seems to be the real problem and does compile with g++, because it’s basically a reinterpret_cast.

    The simplest workaround is changing it to use boost::bind (or std::bind for C++11):

    #include <boost/bind.hpp>
    

    …

    boost::bind(mem_fun(&Foo::tick), _1, &bar)
    

    or a lambda function does give the error you’d expect to see.

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

Sidebar

Related Questions

I guess I would have to use a Highlighter, but I cannot figure it
I guess this is a math question and not a programming question, but what
I guess this is covered somewhere but I'm not able to find. Guess I'm
I guess it's not so hard to do this by hand, but I was
Guess I found it myself: Apparently the else-if block was detected as unreachable code
I guess I have an easy question, I have not found the right answer
I guess the topic says it. I have tried googling this, but havent gotten
I guess my questions are not well described. Is this coding style (like the
I guess starting a timer job from within the code requires Farm admin credentials.
I guess I have to convert the CGRect into an object to pass it

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.