|
Boost : |
From: Thorsten Ottosen (nesotto_at_[hidden])
Date: 2004-04-12 09:26:35
Dear All,
I think I have found a safe way to
bind a temporary to a const reference and cast away the constness.
This means that a temporary can be passed (relatively) safely to an
algorithm
with a prototype like
void foo( T& t )
{
// do something with t, but don't store the reference.
}
for example, we might want to call foo() like
vector<int> make_vec();
foo( make_vec() );
but we cannot. Using a little macro, we can say
foo( BOOST_LVALUE( make_vec() ) );
assuming these prototypes
const vector<int> make_cvec();
const vector<int>& make_crvec();
these uses will fail at ct
foo( BOOST_LVALUE( make_cvec() ) );
foo( BOOST_LVALUE( make_crvec() ) );
Comments are welcome.
br
Thorsten
begin 666 lvalue_cast.cpp
M#0HC:6YC;'5D92 \:6]S=')E86T^#0HC:6YC;'5D92 \=F5C=&]R/@T*(VEN
M8VQU9&4@/&%L9V]R:71H;3X-"@T*=7-I;F<@;F%M97-P86-E('-T9#L-"G5S
M:6YG(&YA;65S<&%C92!B;V]S=#L-"@T*+R\@;75S="!N;W0_at_8F4@<F5J96-T
M960-"G9E8W1O<CQI;G0^('9E8R_at_I#0I[#0H@(" @=F5C=&]R/&EN=#X@=B@@
M,3 L(#$P("D[#0H@(" @<F5T=7)N('8[#0I]#0H-"B\O(&UU<W0_at_8F4@<F5J
M96-T960-"F-O;G-T('9E8W1O<CQI;G0^(&-V96,H*0T*>PT*(" @('9E8W1O
M<CQI;G0^('8H(#$P+" Q," I.PT*(" @(')E='5R;B!V.PT*?0T*#0HO+R!M
M=7-T(&)E(')E:F5C=&5D#0IC;VYS="!V96-T;W(\:6YT/B8_at_8W)V96,H*0T*
M>PT*(" @('-T871I8R!V96-T;W(\:6YT/B!V*" Q,"P@,3 @*3L-"B @("!R
M971U<FX@=CL-"GT-"@T*=&5M<&QA=&4\(&-L87-S(%0@/@T*5"8@;'9A;'5E
M7V-A<W0H(&-O;G-T(%0F(')V86QU92 I#0I[#0H@(" @<F5T=7)N(&-O;G-T
M7V-A<W0\5"8^*"!R=F%L=64@*3L-"GT-"@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;B@@:6YT+"!C:&%R*BH@*0T*>PT*(" @('-O<G0H($)/3U-47TQ604Q5
M12@@=F5C*"D@*2 I.PT*(" @("\O(&-T+65R<F]R._at_T*(" @("\O<V]R="@@
M0D]/4U1?3%9!3%5%*"!C=F5C*"D@*2 I.PT*(" @("\O<V]R="@@0D]/4U1?
H3%9!3%5%*"!C<G9E8R_at_I("D@*3L-"B @("!R971U<FX@,#L-"GT-"@``
`
end
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk