Boost logo

Boost :

Subject: Re: [boost] [type trait extension] I hate volatile...
From: Krzysztof Czainski (1czajnik_at_[hidden])
Date: 2011-07-16 18:42:48

2011/7/16 Frédéric Bron <frederic.bron_at_[hidden]>

> I am still working on the implementation of the traits to detect operators.
> I have an issue with operators returning a volatile object (being a
> reference or not). This breaks the code I wrote to detect if the
> return type is void or not.
> In particular, the following code compiles with RET=int but not with
> RET=ret. Can somebody tell me why?
> It seems that any volatile object (reference or not) cannot be bound
> to a const reference apart for fundamental types. So is there any
> useful case to return a volatile object?
> If not, I can maybe just ignore those cases or it becomes to be a
> nightmare. By the way, it is 1:30am. I am going to bed!
> Frédéric
> struct ret { };
> #define RET int
> RET result;
> RET r() { return result; }
> RET const rc() { return result; }
> RET volatile rv() { return result; } // fails to compile
> RET const volatile rcv() { return result; } // fails to compile
> RET & rr() { return result; }
> RET const & rcr() { return result; }
> RET volatile & rvr() { return result; } // fails to compile
> RET const volatile & rcvr() { return result; } // fails to compile
> int main() {
> RET x;
> x=r();
> x=rc();
> x=rv(); // fails with RET=ret
> x=rcv(); // fails with RET=ret
> x=rr();
> x=rcr();
> x=rvr();
> x=rcvr();
> return 0;
> }


I will present my guess, why your example doesn't compile with RET=ret.. but
first, a question: Does it ever make sense to ever return an object by
value, volatile-qualified?

That said, commenting out the two cases that return volatile-qualified ret
by value, this example still doesn't compile, because the compiler-generated
copy-assign seems to be:
ret& operator=( ret const& ) {}
and can't be used to copy a volatile-qualified object, so an assignment
needs to be added:
ret& operator=( ret const volatile& ) {}
Now this prevent's the compiler from generating it's own copy-assign, so we
also need to add:
ret& operator=( ret const& ) {}
and now all compiles.

I have a feeling, that adding analogous copy constructors would enable
returning volatile-qualified objects by value, but I didn't try doing that,
since I don't see sense in it.

gcc version 4.4.5 (Gentoo 4.4.5 p1.2, pie-0.4.5)


Boost list run by bdawes at, gregod at, cpdaniel at, john at