Boost logo

Boost :

Subject: Re: [boost] [mpl] multiset
From: Ganesh Prasad (sir.gnsp_at_[hidden])
Date: 2015-02-26 07:25:57


On 02/25/2015 11:02 AM, pfultz2 wrote:
> There is no such thing as random access in TMP.

But we can surely imitate a Random Access Container in TMP. Just this
morning it came to my mind that we can make a Compile time random access
container using templates and preprocessor macros. Here is the code, pretty
roughly and quickly written.

  1 #define STATIC_ASSERT(expr) { char assertion_stage[((expr) ? 0:-1)]; }

  2

  3 #define RANDOM_ACCESS_CONTAINER_META(KEY_TYPE, val_type, name) \

  4 template<KEY_TYPE KEY> \

  5 struct meta_container_##name##_{ \

  6 typedef val_type value_type; \

  7 typedef KEY_TYPE key_type; \

  8 static const value_type value = static_cast<value_type>(0); \

  9 }; \

 10 typedef KEY_TYPE meta_container_##name##_key_type_; \

 11 typedef val_type meta_container_##name##_val_type_;

 12

 13 #define RANDOM_ACCESS_INSERT(name, key, val) \

 14 template<> struct meta_container_##name##_<key>{ \

 15 typedef meta_container_##name##_val_type_ value_type; \

 16 typedef meta_container_##name##_key_type_ key_type; \

 17 static const value_type value = val; \

 18 };

 19

 20 #define RANDOM_ACCESS_GET(name, key)
meta_container_##name##_<key>::value
 21

 22 RANDOM_ACCESS_CONTAINER_META(int, int, TMP_INT_MAP);

 23

 24 RANDOM_ACCESS_INSERT(TMP_INT_MAP, 5, 15);

 25 RANDOM_ACCESS_INSERT(TMP_INT_MAP, 6, 18);

 26 RANDOM_ACCESS_INSERT(TMP_INT_MAP, 7, 21);

 27

 28

 29 int main(){

 30

 31 STATIC_ASSERT ( RANDOM_ACCESS_GET(TMP_INT_MAP, 5) == 15 );

 32 STATIC_ASSERT ( RANDOM_ACCESS_GET(TMP_INT_MAP, 6) == 18 );

 33 STATIC_ASSERT ( RANDOM_ACCESS_GET(TMP_INT_MAP, 7) == 21 );

 34 STATIC_ASSERT ( RANDOM_ACCESS_GET(TMP_INT_MAP, 1) == 0 );

 35

 36 return 0;

 37 }

I might be wrong and this might not be the general case, but it's
definitely possible to imitate. After all TMP is turing complete, hence,
random access containers should not be non-existent.

Best wishes
Ganesh Prasad

On 26 February 2015 at 05:20, Larry Evans <cppljevans_at_[hidden]> wrote:

> On 02/25/2015 11:02 AM, pfultz2 wrote:
> >> IMO the fact it mimics the standard library is very positive, for it
> makes
> >> transition much easier for someone used to the STL first experimenting
> >> with
> >> metaprogramming.
> >
> > Actually, that is what has helped me when I started using MPL. However,
> > iterator-based approach for metaprogramming has a huge impact on
> > performance.
> > There is no such thing as random access in TMP.
>
> Not now, but maybe in the future:
>
> http://atpp.irrequietus.eu/atpp-c79f4b7.pdf
>
> -regards,
> Larry
>
>
>
>
> _______________________________________________
> Unsubscribe & other changes:
> http://lists.boost.org/mailman/listinfo.cgi/boost
>


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