Constructor / destructor call mismatches

Discussion in 'C++' started by Dave Theese, Sep 24, 2003.

  1. Dave Theese

    Dave Theese Guest

    Hello all,

    Please see the two questions and output embedded in the program below.

    Thank you!
    Dave


    #ifdef WIN32
    #pragma warning(disable: 4786)
    #endif

    #include <iostream>
    #include <map>

    using namespace std;

    template <int type_differentiator>
    class foo
    {
    public:
    explicit foo(int d): data(d)
    {
    cout << "foo<"
    << type_differentiator
    << ">() : "
    << data
    << endl;
    }

    ~foo()
    {
    cout << "~foo<"
    << type_differentiator
    << ">(): "
    << data
    << endl;
    }

    bool operator<(const foo &lhs) const {return data < lhs.data;}

    private:
    int data;
    };

    int main()
    {
    map<foo<1>, foo<2> > bar;

    cout << "Starting..." << endl;

    // Question 1:
    // How does the line below compile (when not commented out)? foo's
    // constructor is explicit!
    // bar.insert(make_pair(42, 17));

    // Question 2:
    // This line leads to an uneven number of constructor and destructor
    calls.
    // How is this possible?
    bar.insert(
    make_pair(
    foo<1>(42),
    foo<2>(17)
    )
    );

    cout << "Exiting..." << endl;

    return 0;
    }

    // As shown, the program above generates the output below when built with
    // VC++ 6.0:
    //
    // Starting...
    // foo<2>() : 17
    // foo<1>() : 42
    // ~foo<2>(): 17
    // ~foo<1>(): 42
    // ~foo<2>(): 17
    // ~foo<1>(): 42
    // ~foo<1>(): 42
    // ~foo<2>(): 17
    // Exiting...
    // ~foo<2>(): 17
    // ~foo<1>(): 42

    // As shown, the program above generates the output below when built with
    g++:
    //
    // Starting...
    // foo<1>() : 42
    // foo<2>() : 17
    // ~foo<2>(): 17
    // ~foo<1>(): 42
    // ~foo<2>(): 17
    // ~foo<1>(): 42
    // ~foo<2>(): 17
    // ~foo<1>(): 42
    // Exiting...
    // ~foo<2>(): 17
    // ~foo<1>(): 42
     
    Dave Theese, Sep 24, 2003
    #1
    1. Advertising

  2. Dave Theese

    WW Guest

    Dave Theese wrote:
    > Hello all,
    >
    > Please see the two questions and output embedded in the program below.

    [SNIP]
    > template <int type_differentiator>
    > class foo
    > {
    > public:
    > explicit foo(int d): data(d)
    > {
    > cout << "foo<"
    > << type_differentiator
    > << ">() : "
    > << data
    > << endl;
    > }
    >
    > ~foo()
    > {
    > cout << "~foo<"
    > << type_differentiator
    > << ">(): "
    > << data
    > << endl;
    > }
    >
    > bool operator<(const foo &lhs) const {return data < lhs.data;}
    >
    > private:
    > int data;
    > };


    You are missing the copy constructor. Copies never show up as constructed
    but they will show up as destructed.

    --
    WW aka Attila
     
    WW, Sep 24, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gary

    prototype mismatches

    Gary, Jan 7, 2004, in forum: C++
    Replies:
    4
    Views:
    346
  2. Fabian Müller
    Replies:
    23
    Views:
    4,083
    SaltPeter
    Jul 11, 2004
  3. Replies:
    3
    Views:
    193
    Mauricio Fernandez
    Aug 7, 2006
  4. Janus Bor
    Replies:
    3
    Views:
    220
    Harry Kakueki
    Sep 26, 2010
  5. Tim McDaniel

    Detecting sub/call mismatches?

    Tim McDaniel, Nov 21, 2012, in forum: Perl Misc
    Replies:
    3
    Views:
    252
    Rainer Weikusat
    Nov 21, 2012
Loading...

Share This Page