|
Boost : |
Subject: Re: [boost] [static_if] Is there interest in a `static if` emulation library?
From: pfultz2 (pfultz2_at_[hidden])
Date: 2014-08-31 23:35:28
> 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.
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.
Paul
-- View this message in context: http://boost.2283326.n4.nabble.com/static-if-Is-there-interest-in-a-static-if-emulation-library-tp4667118p4667121.html Sent from the Boost - Dev mailing list archive at Nabble.com.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk