Boost logo

Boost :

Subject: Re: [boost] [switch] Is there any interest in library switch extention?
From: Kasra Nassiri\(Math & ComSci\) (kasra_n500_at_[hidden])
Date: 2008-10-01 12:33:29


>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".

      


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk