Re: [boost] sorting library proposal

Is there a good way to issue a compile-time warning telling people that integer_sort or float_sort isn't being used, because they passed in an incompatible data type? I see static assertions, but what I need is an equivalent static warning, after which I use std::sort. integer_sort won't run on long longs, because it uses size_t for distance codes. I've added a check to see whether the key type being used is of the same size or smaller than a size_t to handle this problem. I don't think integer_sort is an appropriate algorithm for data types larger than a size_t, unless comparisons are ridiculously slow, as the k/s factor starts becoming quite large. Also, I'm using a simple if to decided this: if(sizeof(Div_type) <= sizeof(size_t)) because enable_if appears to only work with classes. I'm assuming here that the compiler is smart enough to figure out that the expression is constant and to only bring in the code if it's true. It would be nice to be able to eliminate the compiler warnings about size, and issue a more concise and meaningful message telling the user that their data type is too big for integer_sort (or incompatible with float_sort, as appropriate).

Steven Ross wrote:
Is there a good way to issue a compile-time warning telling people that integer_sort or float_sort isn't being used, because they passed in an incompatible data type? I see static assertions, but what I need is an equivalent static warning, after which I use std::sort.
You can have a look here: http://www.boost.org/doc/libs/1_39_0/libs/serialization/doc/static_warning.h... [snip]
Also, I'm using a simple if to decided this: if(sizeof(Div_type) <= sizeof(size_t)) because enable_if appears to only work with classes.
enable_if does work for functions as well, that's for sure. E.g. template < typename Div_type > typename boost::enable_if_c< sizeof(Div_type) <= sizeof(size_t), void >::type foo( /*args*/ ) { // use optimized algorithm } template < typename Div_type > typename boost::disable_if_c< sizeof(Div_type) <= sizeof(size_t), void >::type foo( /*args*/ ) { // fallback to std::sort BOOST_STATIC_WARNING( sizeof(Div_type) <= sizeof(size_t) ); // BOOST_STATIC_WARNING( false ); won't work because the expression must depend on a template parameter } Or you might consider using tag dispatching to avoid having the condition duplicated in enable_if_c and disable_if_c. [snip] HTH, Regards, Gevorg

On Thu, Jun 4, 2009 at 8:03 AM, Gevorg Voskanyan <v_gevorg@yahoo.com> wrote:
template < typename Div_type > typename boost::disable_if_c< sizeof(Div_type) <= sizeof(size_t), void
::type foo( /*args*/ ) { // fallback to std::sort BOOST_STATIC_WARNING( sizeof(Div_type) <= sizeof(size_t) ); // BOOST_STATIC_WARNING( false ); won't work because the expression must depend on a template parameter }
Thanks Gevorg, that's what I needed. integer_sort is now safe against too-large data types, and the default float_sort is safe against bad casts. The warning message is confusing, but it's a compile-time warning, which is what I need to let the user know that there is a problem. Is it bad for my unit tests to be issuing intentional static warnings?

Steven Ross wrote On Thursday, June 04, 2009 10:41 AM
Is there a good way to issue a compile-time warning telling people that integer_sort or float_sort isn't being used, because they passed in an incompatible data type? I see static assertions, but what I need is an equivalent static warning, after which I use std::sort.
I'm not aware of any.
Also, I'm using a simple if to decided this:
if(sizeof(Div_type) <= sizeof(size_t))
because enable_if appears to only work with classes. I'm
enable_if_c works with any Boolean expression.
assuming here that the compiler is smart enough to figure out that the expression is constant and to only bring in the code if it's true. It would be nice to be able to eliminate the compiler warnings about size, and issue a more concise and meaningful message telling the user that their data type is too big for integer_sort (or incompatible with float_sort, as appropriate).
If you forward to an implementation class template with a bool template parameter defaulted to sizeof(Div_type) <= sizeof(size_t), you can partially specialize for the false case to provide the alternative implementation. Thus, your algorithm invokes a static member function of the implementation class to select the appropriate implementation via compile time dispatch. _____ Rob Stewart robert.stewart@sig.com Software Engineer, Core Software using std::disclaimer; Susquehanna International Group, LLP http://www.sig.com IMPORTANT: The information contained in this email and/or its attachments is confidential. If you are not the intended recipient, please notify the sender immediately by reply and immediately delete this message and all its attachments. Any review, use, reproduction, disclosure or dissemination of this message or any attachment by an unintended recipient is strictly prohibited. Neither this message nor any attachment is intended as or should be construed as an offer, solicitation or recommendation to buy or sell any security or other financial instrument. Neither the sender, his or her employer nor any of their respective affiliates makes any warranties as to the completeness or accuracy of any of the information contained herein or that this message or any of its attachments is free of viruses.
participants (3)
-
Gevorg Voskanyan
-
Steven Ross
-
Stewart, Robert