RAII order of operation

Discussion in 'C++' started by dot, Aug 26, 2008.

  1. dot

    dot Guest

    Let say we have a function:

    class C
    {
    volatile A a;

    public:
    A f() const;
    };

    A C::f()
    {
    volatile scoped_mutex m;
    return a;
    }

    Which of the following operations shall take place first:
    * copy-construction of a
    * destruction of m

    Thanks,
    Ben
    dot, Aug 26, 2008
    #1
    1. Advertising

  2. dot

    Ali Karaali Guest

    On 26 Aðustos, 17:42, dot <"benhongh(at)gmail(dot)com"@nowhere> wrote:
    > Let say we have a function:
    >


    class A
    {
    public :
    A(const A & r)
    { cout << "A::A(const A&)" <<endl; }

    ~A()
    { cout << "A::~A()" << endl; }
    };

    > class C
    > {
    >     volatile A a;
    >
    > public :
    >    
    > A f() const;
    > };
    >
    > A C::f()
    > {
    >     volatile scoped_mutex m;
    >     return a;
    > }
    >
    > Which of the following operations shall take place first:
    >    * copy-construction of a
    >    * destruction of m
    >
    > Thanks,
    > Ben


    You can find now...
    Ali Karaali, Aug 26, 2008
    #2
    1. Advertising

  3. Hi!

    dot schrieb:
    > Let say we have a function:
    >
    > class C
    > {
    > volatile A a;
    >
    > public:
    > A f() const;
    > };
    >
    > A C::f()
    > {
    > volatile scoped_mutex m;
    > return a;
    > }
    >
    > Which of the following operations shall take place first:
    > * copy-construction of a
    > * destruction of m


    None of them is ever called because your code won't compile for several
    reasons. The most important one is that a copy constructor cannot be
    invoked with a volatile object a. The implicit cast from volatile A to
    const A& is not allowed.

    If you remove all the volatile statements (which I would recommend) then
    A's copy constructor is invoked before the destructor of m. However,
    that may not help you because the compiler is allowed to invoke another
    copy constructor on statements like

    A a2 = c.f();

    It is up to the compiler to eliminate the second call to A::A(const A&).


    Marcel
    Marcel Müller, Aug 26, 2008
    #3
  4. dot

    peter koch Guest

    On 26 Aug., 16:42, dot <"benhongh(at)gmail(dot)com"@nowhere> wrote:
    > Let say we have a function:
    >
    > class C
    > {
    >     volatile A a;
    >
    >    public:
    >     A f() const;
    >
    > };
    >
    > A C::f()
    > {
    >     volatile scoped_mutex m;
    >     return a;
    >
    > }
    >
    > Which of the following operations shall take place first:
    >    * copy-construction of a
    >    * destruction of m


    copy-construction of a. This is guaranteed by the standard, so no need
    to test.

    /Peter
    peter koch, Aug 26, 2008
    #4
  5. dot

    peter koch Guest

    On 26 Aug., 16:42, dot <"benhongh(at)gmail(dot)com"@nowhere> wrote:
    > Let say we have a function:
    >
    > class C
    > {
    >     volatile A a;
    >
    >    public:
    >     A f() const;
    >
    > };
    >
    > A C::f()
    > {
    >     volatile scoped_mutex m;
    >     return a;
    >
    > }
    >


    As Marcel Müller mentioned, there is no need for using volatile here -
    neither for the mutex nor for a. And you also miss a const on the
    definition of C::f.

    /Peter
    peter koch, Aug 26, 2008
    #5
    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. david ullua
    Replies:
    13
    Views:
    640
  2. raan
    Replies:
    2
    Views:
    435
  3. Johannes Schaub (litb)

    Re: Why is RAII called RAII?

    Johannes Schaub (litb), Sep 12, 2010, in forum: C++
    Replies:
    2
    Views:
    387
    James Kanze
    Sep 18, 2010
  4. cpp4ever

    Re: Why is RAII called RAII?

    cpp4ever, Sep 12, 2010, in forum: C++
    Replies:
    1
    Views:
    393
    BGB / cr88192
    Sep 13, 2010
  5. Goran Pusic

    Re: Why is RAII called RAII?

    Goran Pusic, Sep 13, 2010, in forum: C++
    Replies:
    11
    Views:
    534
    ptyxs
    Sep 16, 2010
Loading...

Share This Page