Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   RAII order of operation (http://www.velocityreviews.com/forums/t632464-raii-order-of-operation.html)

dot 08-26-2008 02:42 PM

RAII order of operation
 
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

Ali Karaali 08-26-2008 07:45 PM

Re: RAII order of operation
 
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...

Marcel Müller 08-26-2008 08:07 PM

Re: RAII order of operation
 
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

peter koch 08-26-2008 08:10 PM

Re: RAII order of operation
 
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 08-26-2008 08:15 PM

Re: RAII order of operation
 
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


All times are GMT. The time now is 02:28 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.