|
Boost : |
From: Eric Niebler (eric_at_[hidden])
Date: 2005-12-17 00:17:18
Eric Niebler wrote:
> Arkadiy Vertleyb wrote:
>
>>What is current state of the art on l-value/r-value detection? (I know Eric
>>made some progress in the past by using the "?" operator).
>>
<snip>
>
> It's an open question whether it's possible to do this in standard C++.
> If it's possible, I'd be very interested.
Here's my latest attempt. This works on gcc 3.4.4 (cygwin), but not on
VC7.1 or (sadly) Comeau Online. Language lawyers, start your engines!
template<typename T>
struct probe
{
operator T ();
operator T volatile const &() const;
};
template<typename T>
probe<T> make_probe(T const &);
template<typename T>
char is_volatile(T const &);
template<typename T>
double is_volatile(T const volatile &);
#define IS_RVALUE(x) \
(\
sizeof(char) == \
sizeof(is_volatile(true?make_probe(x):(x)))\
)
// tests
struct foo { foo() {} };
foo const make_foo() { return foo(); }
int main()
{
int i;
foo const f1;
foo f2;
char t1[!IS_RVALUE(f1)];
char t2[!IS_RVALUE(f2)];
char t3[IS_RVALUE(foo())];
char t4[IS_RVALUE(make_foo())];
char t5[!IS_RVALUE(i)];
char t6[IS_RVALUE(1)];
return 0;
}
-- Eric Niebler Boost Consulting www.boost-consulting.com
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk