
While examining source and binary code for the various safe_bool/unspecified_bool_type implementations two issues can be noticed: - source code verbosity/repetition - binary code inefficiency The first issue is made worse by the fact that even this simple idiom is broken on some compilers ('pointed out' by workarounds in smart_ptr/detail/operator_bool.hpp) so workarounds must also be copied/'rediscovered'... The second issue (related to the usual implementation with member function pointers) has two sides, one is that taking the address of a function will force it to be included in the resulting binary even if it is never used or is always inlined and the second is that some compilers produce non-optimal code. For example for the following source code: boost::optional<int> test1; boost::optional<int> test2; __declspec( noinline ) void func() { if ( test1 ) test2 = *test1; } MSVC++ 8.0 and 9.0 will produce the following binary code ... 0F923A26 movzx eax,byte ptr [test1 (0F968210h)] 0F923A2D neg eax 0F923A2F sbb eax,eax 0F923A31 test eax,offset boost::optional_detail::optional_base<int>::is_initialized (0F923B9Eh) 0F923A36 je ... ... but if the if line is changed to if ( test1.is_initialized() ) the following code will be produced: ... 0F923A26 cmp byte ptr [test1 (0F968210h)],0 0F923A2D je ... ... At first I tried a solution that takes advantage of the fact that MSVC++ has plain-pointer-sized/register-sized member function pointers for plain single inheritance non virtual member functions and worked properly but than I found old discussions about the same subject: http://lists.boost.org/Archives/boost/2003/09/52846.php http://lists.boost.org/Archives/boost/2002/10/38624.php where the solution for MSVC++ was to use a pointer member data which gives the same results as my initial solution... Anyways I've attached a .hpp file with a draft implementation proposal (missing the compiler workarounds from smart_ptr/smart_ptr/detail/operator_bool.hpp for example) that uses both approaches... It enables a one liner implementation for a safe_bool operator: class my_class { public: bool some_is_valid_function() const; operator boost::unspecified_bool_type() const { return boost::make_safe_bool( some_is_valid_function() ); } //or by using a macro BOOST_SAFE_BOOL_FROM_FUNCTION( some_is_valid_function() ) } -- "What Huxley teaches is that in the age of advanced technology, spiritual devastation is more likely to come from an enemy with a smiling face than from one whose countenance exudes suspicion and hate." Neil Postman begin 666 safe_bool.hpp M(W!R86=M82!O;F-E#0HC:69N9&5F('-A9F5?8F]O;%]H<' -"B-D969I;F4@ M<V%F95]B;V]L7VAP< T*+R\M+2TM+2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92!C;VUP:6QE<B!I<R!A8FQE('1O M(&9I="!A('!L86EN+"!S:6YG;&4-"B @(" O+R!I;FAE<FET86YC92!M96UB M97(@9G5N8W1I;VX@<&]I;G1E<B!I;G1O('-I>F5O9B@@=F]I9" J("D@=&AA M="!I=',@;G5L; T*(" @("\O(&)I;F%R>2!R97!R97-E;G1A=&EO;B!I<R!I M9&5N=&EC86P@=&\@82!P;&%I;B!N=6QL('9O:60@<&]I;G1E<B H86QL(&)I M=',-"B @(" O+R!Z97)O960I+B!7:71H;W5T(&$@=V%Y('1O(&-H96-K('1H M:7,@870@8V]M<&EL92!T:6UE('1H:7,@87-S97)T960@870-"B @(" O+R!R M=6YT:6UE+@T*(" @("\O(" @5&AE(&%B;W9E(&YE960@;F]T(&AO;&0@9F]R M(&1A=&$@;65M8F5R('!O:6YT97)S("AE+F<N($U35D,K*R!U<V5S("TQ#0H@ M(" @+R\@9F]R(&YU;&PM9&%T82!M96UB97(@<&]I;G1E<G,I+@T*(" @('1Y M<&5D968@;7!L.CIB;V]L7SPH('-I>F5O9B@@9F%S=%]S869E7V)O;VP@*2 \ M/2!S:7IE;V8H('5N<VEG;F5D(&QO;F<@*2 I/B!C86Y?=7-E7V9A<W1?8F]O M;%]H86-K.PT*#0H@(" @='EP961E9B!M<&PZ.FEF7PT*(" @(" @(" @(" @ M/ T*(" @(" @(" @(" @(" @(&-A;E]U<V5?9F%S=%]B;V]L7VAA8VLL#0H@ M(" @(" @(" @(" @(" @=6YS<&5C:69I961?8F]O;%]T>7!E7V9U;F-T:6]N M+ T*(" @(" @(" @(" @(" @('5N<W!E8VEF:65D7V)O;VQ?='EP95]D871A M#0H@(" @(" @(" @(" ^.CIT>7!E('5N<W!E8VEF:65D7V)O;VQ?='EP93L- M"@T*(" @(&EN;&EN90T*(" @('5N<W!E8VEF:65D7V)O;VQ?='EP92!M86ME M7W-A9F5?8F]O;%]W;W)K97(H(&)O;VP@8V]N<W0@=F%L=64L(&UP;#HZ=')U M95\@+RIU<V4@9F%S="UH86-K('9E<G-I;VXJ+R I#0H@(" @>PT*(" @(" @ M("!F87-T7W-A9F5?8F]O;"!C;VYS="!F87-T4V%F94)O;VP@/2![('9A;'5E M('T[#0H@(" @(" @(&%S<V5R= T*(" @(" @(" H#0H@(" @(" @(" @(" H M("$A9F%S=%-A9F5";V]L+G!O:6YT97)?=&]?;65M8F5R(#T]("$A=F%L=64@ M*2 F)@T*(" @(" @(" @(" @(E1H92!V;VED+7!O:6YT97(M<VEZ960@;65M M8F5R('!O:6YT97(@;G5L;"!B:6YA<GDB#0H@(" @(" @(" @(" B<F5P<F5S M96YT871I;VX@87-S=6UP=&EO;B!D;V5S(&YO="!H;VQD(&9O<B!T:&ES(@T* M(" @(" @(" @(" @(F-O;7!I;&5R+W!L871F;W)M+B(-"B @(" @(" @*3L- M"B @(" @(" @<F5T=7)N(&9A<W13869E0F]O;"YP;VEN=&5R7W1O7VUE;6)E M<CL-"B @("!]#0H-"B @("!I;FQI;F4-"B @("!U;G-P96-I9FEE9%]B;V]L M7W1Y<&5?9G5N8W1I;VX-"B @("!M86ME7W-A9F5?8F]O;%]S=&%N9&%R9%]W M;W)K97(H(&)O;VP@8V]N<W0@8F]O;%]V86QU92P@=6YS<&5C:69I961?8F]O M;%]T>7!E7V9U;F-T:6]N(&-O;G-T(&YU;&Q?=F%L=64@*0T*(" @('L-"B @ M(" @(" @<F5T=7)N(&)O;VQ?=F%L=64@/R F=6YS<&5C:69I961?8F]O;%]T M>7!E7VAE;'!E<CHZ;65M8F5R7V9U;F-T:6]N(#H@;G5L;%]V86QU93L-"B @ M("!]#0H@(" @:6YL:6YE#0H@(" @=6YS<&5C:69I961?8F]O;%]T>7!E7V1A M=&$-"B @("!M86ME7W-A9F5?8F]O;%]S=&%N9&%R9%]W;W)K97(H(&)O;VP@ M8V]N<W0@8F]O;%]V86QU92P@=6YS<&5C:69I961?8F]O;%]T>7!E7V1A=&$@ M(" @(&-O;G-T(&YU;&Q?=F%L=64@*0T*(" @('L-"B @(" @(" @<F5T=7)N M(&)O;VQ?=F%L=64@/R F=6YS<&5C:69I961?8F]O;%]T>7!E7VAE;'!E<CHZ M;65M8F5R7V1A=&%?(" @(#H@;G5L;%]V86QU93L-"B @("!]#0H-"B @("!I M;FQI;F4-"B @("!U;G-P96-I9FEE9%]B;V]L7W1Y<&4@;6%K95]S869E7V)O M;VQ?=V]R:V5R*"!B;V]L(&-O;G-T('9A;'5E+"!M<&PZ.F9A;'-E7R O*G5S M92!S=&%N9&%R9"!V97)S:6]N*B\@*0T*(" @('L-"B @(" @(" @<F5T=7)N M(&UA:V5?<V%F95]B;V]L7W-T86YD87)D7W=O<FME<B@@=F%L=64L('5N<W!E M8VEF:65D7V)O;VQ?='EP92@@," I("D[#0H@(" @?0T*?2 O+R!N86UE<W!A M8V4@9&5T86EL#0H-"@T*='EP961E9B!D971A:6PZ.G5N<W!E8VEF:65D7V)O M;VQ?='EP92!U;G-P96-I9FEE9%]B;V]L7W1Y<&4[#0H-"@T*:6YL:6YE#0IU M;G-P96-I9FEE9%]B;V]L7W1Y<&4@;6%K95]S869E7V)O;VPH(&)O;VP@8V]N M<W0@=F%L=64@*0T*>PT*(" @(')E='5R;B!D971A:6PZ.FUA:V5?<V%F95]B M;V]L7W=O<FME<B@@=F%L=64L(&1E=&%I;#HZ8V%N7W5S95]F87-T7V)O;VQ? M:&%C:R@I("D[#0I]#0H-"B\O($EF('=E(&-O=6QD('5S92!M96UB97(@<&]I M;G1E<G,@8F5F;W)E('1H92!M96UB97(@:7,@86-T=6%L;'D@9&5C;&%R960O M<V5E;@T*+R\@=V4@8V]U;&0@=7-E($-25% M8F%S960@:&5L<&5R(&-L87-S M97,@9F]R(&UI;FEM=6T@=F5R8F]S:71Y('-A9F5?8F]O; T*+R\@:6UP;&5M M96YT871I;VYS.@T*+R\@=&5M<&QA=&4@/&-L87-S($)A<V4L(&)O;VP@*$)A M<V4Z.BIB;V]L7V9U;F-T:6]N*2 H*2!C;VYS=#X-"B\O(&-L87-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M+2TM+2TM+2TM+2T-"B-E 7;F1I9B O+R!S869E7V)O;VQ?:'!P#0H` ` end

Domagoj Saric wrote:
It enables a one liner implementation for a safe_bool operator:
class my_class { public: bool some_is_valid_function() const; operator boost::unspecified_bool_type() const { return boost::make_safe_bool( some_is_valid_function() ); }
Using a single type allows unrelated classes to compare equal with operator==.

"Peter Dimov" <pdimov@pdimov.com> wrote in message news:A062A9144EEA45339956BFD0D4FDC906@pdimov2...
Using a single type allows unrelated classes to compare equal with operator==.
Do'h :) Even if I thought of that I would have thought that the 'only-a-single-conversion-may-take-place' rule would apply...Thanks for the correction ;) ...but still the proposal can work, only in the usual template-uglified form: class my_class { public: bool some_is_valid_function() const; operator boost::safe_bool<my_class>::type() const { return boost::safe_bool<my_class>::make( some_is_valid_function() ); } //or by using a macro BOOST_SAFE_BOOL_FROM_FUNCTION( my_class, some_is_valid_function() ); //or by using a data member unsigned int size_; operator boost::safe_bool<my_class>::type() const { return boost::safe_bool<my_class>::make( size_ ); } BOOST_SAFE_BOOL_FROM_DATA( my_class, size_ ); } ...still less hassle than status quo... ...or maybe even more so if we take advantage of the fact that now we can use the CRTP idiom (or part of it): class my_class : boost::safe_bool<my_class> { public: bool some_is_valid_function() const; operator unspecified_bool_type() const { return make( some_is_valid_function() ); } } -- "What Huxley teaches is that in the age of advanced technology, spiritual devastation is more likely to come from an enemy with a smiling face than from one whose countenance exudes suspicion and hate." Neil Postman begin 666 safe_bool.hpp M(W!R86=M82!O;F-E#0HC:69N9&5F('-A9F5?8F]O;%]H<' -"B-D969I;F4@ M<V%F95]B;V]L7VAP< T*+R\M+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM M+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM M+2TM+2T-"B-I;F-L=61E(")B;V]S="]M<&PO8F]O;"YH<' B#0HO+RTM+2TM M+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM M+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+0T*;F%M97-P86-E(&)O;W-T M#0I[#0HO+RTM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM M+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+0T*#0IT M96UP;&%T92 \8VQA<W,@5#X-"G-T<G5C="!S869E7V)O;VP-"GL-"G!R:79A M=&4Z#0H@(" @<W1R=6-T('5N<W!E8VEF:65D7V)O;VQ?='EP95]H96QP97(- M"B @("![#0H@(" @(" @('9O:60@;65M8F5R7V9U;F-T:6]N*"D@>WT[#0H@ M(" @(" @(&EN="!M96UB97)?9&%T85\[#0H@(" @?3L-"@T*(" @('1Y<&5D M968@=F]I9" H=6YS<&5C:69I961?8F]O;%]T>7!E7VAE;'!E<CHZ*G5N<W!E M8VEF:65D7V)O;VQ?='EP95]F=6YC=&EO;BD@*"D[#0H@(" @='EP961E9B!I M;G0@("!U;G-P96-I9FEE9%]B;V]L7W1Y<&5?:&5L<&5R.CHJ=6YS<&5C:69I M961?8F]O;%]T>7!E7V1A=&$@(" @(" @(#L-"@T*(" @('5N:6]N(&9A<W1? M<V%F95]B;V]L#0H@(" @>PT*(" @(" @("!U;G-I9VYE9"!L;VYG(" @(" @ M(" @(" @(" @(" @<&QA:6Y?<&]I;G1E<E]P;&%C96AO;&1E<CL-"B @(" @ M(" @=6YS<&5C:69I961?8F]O;%]T>7!E7V9U;F-T:6]N('!O:6YT97)?=&]? M;65M8F5R(" @(" @(" [#0H@(" @?3L-"@T*(" @("\O(" @270@:7,@87-S M=6UE9"!T:&%T(&EF('1H92!C;VUP:6QE<B!I<R!A8FQE('1O(&9I="!A('!L M86EN+"!S:6YG;&4-"B @(" O+R!I;FAE<FET86YC92!M96UB97(@9G5N8W1I M;VX@<&]I;G1E<B!I;G1O('-I>F5O9B@@=F]I9" J("D@=&AA="!I=',@;G5L M; T*(" @("\O(&)I;F%R>2!R97!R97-E;G1A=&EO;B!I<R!I9&5N=&EC86P@ M=&\@82!P;&%I;B!N=6QL('9O:60@<&]I;G1E<B H86QL(&)I=',-"B @(" O M+R!Z97)O960I+B!7:71H;W5T(&$@=V%Y('1O(&-H96-K('1H:7,@870@8V]M M<&EL92!T:6UE('1H:7,@87-S97)T960@870-"B @(" O+R!R=6YT:6UE+@T* M(" @("\O(" @5&AE(&%B;W9E(&YE960@;F]T(&AO;&0@9F]R(&1A=&$@;65M M8F5R('!O:6YT97)S("AE+F<N($U35D,K*R!U<V5S("TQ#0H@(" @+R\@9F]R M(&YU;&PM9&%T82!M96UB97(@<&]I;G1E<G,I+@T*(" @('1Y<&5D968@;7!L M.CIB;V]L7PT*(" @(" @(" \#0H@(" @(" @(" @(" H('-I>F5O9B@@9F%S M=%]S869E7V)O;VP@*2 \/2!S:7IE;V8H('5N<VEG;F5D(&QO;F<@*2 I#0H@ M(" @(" @(#X@8V%N7W5S95]F87-T7V)O;VQ?:&%C:SL-"@T*<')O=&5C=&5D M.@T*(" @('1Y<&5D968@='EP96YA;64@;7!L.CII9E\-"B @(" @(" @/ T* M(" @(" @(" @(" @8V%N7W5S95]F87-T7V)O;VQ?:&%C:RP-"B @(" @(" @ M(" @('5N<W!E8VEF:65D7V)O;VQ?='EP95]F=6YC=&EO;BP-"B @(" @(" @ M(" @('5N<W!E8VEF:65D7V)O;VQ?='EP95]D871A#0H@(" @(" @(#XZ.G1Y M<&4@=6YS<&5C:69I961?8F]O;%]T>7!E.PT*#0IP<FEV871E.@T*(" @('-T M871I8PT*(" @('5N<W!E8VEF:65D7V)O;VQ?='EP95]F=6YC=&EO;@T*(" @ M(&UA:V5?<V%F95]B;V]L7W-T86YD87)D7W=O<FME<B@@8F]O;"!C;VYS="!B M;V]L7W9A;'5E+"!U;G-P96-I9FEE9%]B;V]L7W1Y<&5?9G5N8W1I;VX@8V]N M<W0@;G5L;%]V86QU92 I#0H@(" @>PT*(" @(" @("!R971U<FX@8F]O;%]V M86QU92 _("9U;G-P96-I9FEE9%]B;V]L7W1Y<&5?:&5L<&5R.CIM96UB97)? M9G5N8W1I;VX@.B!N=6QL7W9A;'5E.PT*(" @('T-"B @("!S=&%T:6,-"B @ M("!U;G-P96-I9FEE9%]B;V]L7W1Y<&5?9&%T80T*(" @(&UA:V5?<V%F95]B M;V]L7W-T86YD87)D7W=O<FME<B@@8F]O;"!C;VYS="!B;V]L7W9A;'5E+"!U M;G-P96-I9FEE9%]B;V]L7W1Y<&5?9&%T82 @(" @8V]N<W0@;G5L;%]V86QU M92 I#0H@(" @>PT*(" @(" @("!R971U<FX@8F]O;%]V86QU92 _("9U;G-P M96-I9FEE9%]B;V]L7W1Y<&5?:&5L<&5R.CIM96UB97)?9&%T85\@(" @.B!N M=6QL7W9A;'5E.PT*(" @('T-"@T*(" @('-T871I8PT*(" @('5N<W!E8VEF M:65D7V)O;VQ?='EP92!M86ME7W-A9F5?8F]O;%]W;W)K97(H(&)O;VP@8V]N M<W0@=F%L=64L(&UP;#HZ9F%L<V5?("\J=7-E('-T86YD87)D('9E<G-I;VXJ M+R I#0H@(" @>PT*(" @(" @("!R971U<FX@;6%K95]S869E7V)O;VQ?<W1A M;F1A<F1?=V]R:V5R*"!V86QU92P@=6YS<&5C:69I961?8F]O;%]T>7!E*" P M("D@*3L-"B @("!]#0H-"B @("!S=&%T:6,-"B @("!U;G-P96-I9FEE9%]B M;V]L7W1Y<&4@;6%K95]S869E7V)O;VQ?=V]R:V5R*"!B;V]L(&-O;G-T('9A M;'5E+"!M<&PZ.G1R=65?("\J=7-E(&9A<W0M:&%C:R!V97)S:6]N*B\@*0T* M(" @('L-"B @(" @(" @9F%S=%]S869E7V)O;VP@8V]N<W0@9F%S=%-A9F5" M;V]L(#T@>R!V86QU92!].PT*(" @(" @("!A<W-E<G0-"B @(" @(" @* T* M(" @(" @(" @(" @*" A(69A<W13869E0F]O;"YP;VEN=&5R7W1O7VUE;6)E M<B ]/2 A(79A;'5E("D@)B8-"B @(" @(" @(" @(")4:&4@=F]I9"UP;VEN M=&5R+7-I>F5D(&UE;6)E<B!P;VEN=&5R(&YU;&P@8FEN87)Y(@T*(" @(" @ M(" @(" @(G)E<')E<V5N=&%T:6]N(&%S<W5M<'1I;VX@9&]E<R!N;W0@:&]L M9"!F;W(@=&AI<R(-"B @(" @(" @(" @(")C;VUP:6QE<B]P;&%T9F]R;2XB M#0H@(" @(" @("D[#0H@(" @(" @(')E='5R;B!F87-T4V%F94)O;VPN<&]I M;G1E<E]T;U]M96UB97([#0H@(" @?0T*#0IP=6)L:6,Z#0H@(" @='EP961E M9B!U;G-P96-I9FEE9%]B;V]L7W1Y<&4@='EP93L-"@T*(" @('1E;7!L871E M(#QT>7!E;F%M92!I;7!L:6-I=%]B;V]L/@T*(" @('-T871I8R!T>7!E(&UA M:V4H(&EM<&QI8VET7V)O;VP@8V]N<W0@=F%L=64@*0T*(" @('L-"B @(" @ M(" @<F5T=7)N(&UA:V5?<V%F95]B;V]L7W=O<FME<B@@(2%V86QU92P@8V%N M7W5S95]F87-T7V)O;VQ?:&%C:R@I("D[#0H@(" @?0T*?3L@+R\@;F%M97-P M86-E(&1E=&%I; T*#0H-"@T*#0HO+R!)9B!W92!C;W5L9"!U<V4@;65M8F5R M('!O:6YT97)S(&)E9F]R92!T:&4@;65M8F5R(&ES(&%C='5A;&QY(&1E8VQA M<F5D+W-E96X-"B\O('=E(&-O=6QD('5S92!#4E10+6)A<V5D(&AE;'!E<B!C M;&%S<V5S(&9O<B!M:6YI;75M('9E<F)O<VET>2!S869E7V)O;VP-"B\O(&EM M<&QE;65N=&%T:6]N<SH-"B\O('1E;7!L871E(#QC;&%S<R!"87-E+"!B;V]L M("A"87-E.CHJ8F]O;%]F=6YC=&EO;BD@*"D@8V]N<W0^#0HO+R!C;&%S<R!S M869E7V)O;VP-"B\O('L-"B\O(" @("!O<&5R871O<B!U;G-P96-I9FEE9%]B M;V]L7W1Y<&4H*2!C;VYS="![(')E='5R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+2TM+2TM F+2TM+2TM+2TM+2T-"B-E;F1I9B O+R!S869E7V)O;VQ?:'!P#0H` ` end

On 1 March 2010 15:36, Domagoj Saric <domagoj.saric@littleendian.com> wrote:
//or by using a macro BOOST_SAFE_BOOL_FROM_FUNCTION( some_is_valid_function() )
Is there a reason it can't just always look at operator!? I've been using a macro like this, since I don't need the workarounds: #define OPERATOR_SAFE_BOOL(for_type) \ typedef bool (for_type::*unspecified_bool_type)() const; \ operator unspecified_bool_type() const { \ return !*this ? 0 : &for_type::operator!; \ } so then it's just class my_class { public: bool operator!() const; OPERATOR_SAFE_BOOL(my_class) };

"Scott McMurray" <me22.ca+boost@gmail.com> wrote in message news:fa28b9251003011306k77a61047l4e0236ebbd6d8dc3@mail.gmail.com...
Is there a reason it can't just always look at operator!?
I've been using a macro like this, since I don't need the workarounds:
#define OPERATOR_SAFE_BOOL(for_type) \ typedef bool (for_type::*unspecified_bool_type)() const; \ operator unspecified_bool_type() const { \ return !*this ? 0 : &for_type::operator!; \ }
This forces classes to have operator! and it still suffers from efficiency issues outlined in the first post... -- "What Huxley teaches is that in the age of advanced technology, spiritual devastation is more likely to come from an enemy with a smiling face than from one whose countenance exudes suspicion and hate." Neil Postman

Domagoj Saric wrote:
"Scott McMurray" <me22.ca+boost@gmail.com> wrote in message news:fa28b9251003011306k77a61047l4e0236ebbd6d8dc3@mail.gmail.com...
Is there a reason it can't just always look at operator!?
I've been using a macro like this, since I don't need the workarounds:
#define OPERATOR_SAFE_BOOL(for_type) \ typedef bool (for_type::*unspecified_bool_type)() const; \ operator unspecified_bool_type() const { \ return !*this ? 0 : &for_type::operator!; \ }
This forces classes to have operator! and it still suffers from efficiency issues outlined in the first post...
I don't think the requirement for operator! is so bad (should certainly satisfy the majority of use cases, right?), and perhaps efficiency can be addressed by defining the "unspecified_bool_type" conversion in debug builds and a straight bool conversion in release builds for "problem" compilers...? Or perhaps some other preprocessor macro can direct whether to go with the type-safe version (default) or the efficient version (which may in fact be the same for some compilers)...? - Jeff

"Jeffrey Hellrung" <jhellrung@ucla.edu> wrote in message news:4B8F777C.6000202@ucla.edu...
I don't think the requirement for operator! is so bad (should certainly satisfy the majority of use cases, right?),
FWIW I don't remember writing one, ever... ;)
and perhaps efficiency can be addressed by defining the "unspecified_bool_type" conversion in debug builds and a straight bool conversion in release builds for "problem" compilers...? Or perhaps some other preprocessor macro can direct whether to go with the type-safe version (default) or the efficient version (which may in fact be the same for some compilers)...?
But there is no need for any of that...shared_ptr already has an efficient implementation... I just wrapped this implementation along with one of my one (that is perhaps slightly less 'clean' but easier on the optimizer) into a 'reusable' solution... -- "What Huxley teaches is that in the age of advanced technology, spiritual devastation is more likely to come from an enemy with a smiling face than from one whose countenance exudes suspicion and hate." Neil Postman

Domagoj Saric wrote:
But there is no need for any of that...shared_ptr already has an efficient implementation... I just wrapped this implementation along with one of my one (that is perhaps slightly less 'clean' but easier on the optimizer) into a 'reusable' solution...
User testimonial: in my own boost absed library when i have a pointer like class and i want the safe bool conversion, I make it so my members are named in such a way tha including the shared_ptr safe_bool header is all I need. Never had any problem

"Joel Falcou" <joel.falcou@lri.fr> wrote in message news:4B8FAD36.1020500@lri.fr...
User testimonial: in my own boost absed library when i have a pointer like class and i want the safe bool conversion, I make it so my members are named in such a way tha including the shared_ptr safe_bool header is all I need. Never had any problem
Isn't that actually yet another 'pointer' that a clean, public, reusable utility component is needed for this 'age old' idiom? -- "What Huxley teaches is that in the age of advanced technology, spiritual devastation is more likely to come from an enemy with a smiling face than from one whose countenance exudes suspicion and hate." Neil Postman

On Fri, Mar 5, 2010 at 9:18 AM, Domagoj Saric < domagoj.saric@littleendian.com> wrote:
User testimonial: in my own boost absed library when i have a pointer
"Joel Falcou" <joel.falcou@lri.fr> wrote in message news:4B8FAD36.1020500@lri.fr... like
class and i want the safe bool conversion, I make it so my members are named in such a way tha including the shared_ptr safe_bool header is all I need. Never had any problem
Isn't that actually yet another 'pointer' that a clean, public, reusable utility component is needed for this 'age old' idiom?
Yes, it is more supporting data that we need a reusable bool conversion mechanism. I am finding that I need this in Boost.Range (since the implementation in here is sub-optimal). I am also finding in my other application development that a general utility for providing such a mechanism would be extremely useful. I intend to develop such a reusable Boost-ified macro and get it into the core of Boost. Any objections, or comments? Regards, Neil Groves
participants (7)
-
Domagoj Saric
-
Domagoj Saric
-
Jeffrey Hellrung
-
Joel Falcou
-
Neil Groves
-
Peter Dimov
-
Scott McMurray