Re: [boost] [switch] Is there any interest in library switch extention?

I hardly see how this could work for a O(1) lookup (the point of the switch statement).
I have found a compile time solution! However, we need C++0x :( A hash functions with output type of <int> so we could embed the statement in a normal switch statement on C++0x. #define BOOST_SWITCH( object ) switch ( const_hash_function(object) ) #define BOOST_CASE( case_object ) case const_hash_function(case_object) #define BOOST_DEFAULT default So we could write code like: BOOST_SWITCH( "Hello World" ) { BOOST_CASE("wrong value"): BOOST_CASE("another wrong value"): cout << "Reached wrong statements" << endl; BOOST_BREAK; BOOST_DEFAULT: cout << "Hello World" << endl; }; What I like to add is a reference to the value of the BOOST_SWITCH(...), in this example "Hello World".

On Wed, Oct 1, 2008 at 6:33 PM, Kasra Nassiri(Math & ComSci) <kasra_n500@yahoo.com> wrote:
I hardly see how this could work for a O(1) lookup (the point of the switch statement).
I have found a compile time solution! However, we need C++0x :( A hash functions with output type of <int> so we could embed the statement in a normal switch statement on C++0x.
so const_hash_function is a constexpr? Still, if the case values are not contiguous, most compilers will require at best logN lookups (they could do better with perfect hashing, but is there any compiler that actually does it?), so the improvement is not that great. Also, on current hardware, an O(1) lookup done with and indirect jump is not necessarily better than O(n) lookup done with conditional branches (in fact it might be much worse), for small values of n, at least. -- gpd
participants (2)
-
Giovanni Piero Deretta
-
Kasra Nassiri(Math & ComSci)