Boost logo

Boost :

Subject: Re: [boost] [static_if] Is there interest in a `static if` emulation library?
From: TONGARI J (tongari95_at_[hidden])
Date: 2014-09-01 01:50:48


2014-09-01 11:35 GMT+08:00 pfultz2 <pfultz2_at_[hidden]>:

> > Another option is to always pass a dummy arg, and required the lambda to
> > accept a param. e.g. `[](auto){....}`, so the verbosity of std::bind can
> > be
> > avoided.
>
> Its not enough for it to be lazy, since accessing non-dependent types still
> have to compile. That is why Lorenzo passes them in as template parameters.
>

You're right!

Another way accomplishing this is to pass in an identity function:
>
> namespace aux {
> struct identity
> {
> template<class T>
> T operator()(T&& x) const
> {
> return std::forward<T>(x);
> }
> };
>
> template< bool Condition >
> struct static_if_statement
> {
> template< typename F > void then ( F const& f ) { f(identity()); }
> template< typename F > void else_ ( F const& ) { }
> };
>
> template< >
> struct static_if_statement<false>
> {
> template< typename F > void then ( F const& ) { }
> template< typename F > void else_ ( F const& f ) { f(identity()); }
> };
> }
>
> template< bool Condition, typename F >
> aux::static_if_statement<Condition> static_if ( F const& f )
> {
> aux::static_if_statement<Condition> if_;
> if_.then(f);
> return if_;
> }
>
> Then you can call it like this:
>
> template< typename T >
> void assign ( T& x, T const& y )
> {
> x = y;
>
> static_if<boost::has_equal_to<T>::value>([](auto f)
> {
> assert(f(x) == f(y));
> std::cout << "asserted for: " << typeid(T).name() << std::endl;
> })
> .else_([] (auto)
> {
> std::cout << "cannot assert for: " << typeid(T).name() <<
> std::endl;
> });
> }
>
> The identity function will make non-dependent types dependent, so this will
> compile.
>

Good point, maybe we can call f `lazy` here.


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