Boost logo

Boost :

Subject: [boost] boost::any_ref and boost::any_const_ref
From: Daniel Larimer (dlarimer_at_[hidden])
Date: 2010-02-14 14:23:11


I am attempting to create a library with a generic interface run-time interface and have need for something like boost::any; however, boost::any requires heap allocation. boost::variant is not acceptable either because I do not know the full set of types in advance.

Has anyone seen the equivalent of a type-checked void* or const void*?

Effectively I would like to create a polymorphic method like this:

void myclass::method( const_any_ref p1, const_any_ref p2, const any_ref p3 )
{
        const uint16_t& _p1 = p1;
        const double& _p2 = p2;
        const std::string& _p3 = p3;

        //If any of the types do not match it should throw an exception.

        alternatively the follow syntax may work if you want to avoid exceptions?
        boost::optional<const uint16_t&> _p1 = p1;
        if( !_p1 ) // cast failed?
}

Anyone have any nifty ideas on how to accomplish this without RTTI and no heap allocation?

In place of RTTI I do have a "manual" RTTI system that I could use.

get_typename<T>::str will return a unique const char* pointer.

I am guessing the best I can hope for is a pair of pointers, one "void*" and one const char*.

I am going to write such a class unless someone here knows of an existing solution.

Dan


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