|
Boost : |
Subject: Re: [boost] [static_if] Is there interest in a `static if` emulation library?
From: TONGARI J (tongari95_at_[hidden])
Date: 2014-08-31 22:23:33
Hi Lorenzo,
2014-09-01 9:37 GMT+08:00 Lorenzo Caminiti <lorcaminiti_at_[hidden]>:
> Hello all,
>
> With C++14 generic lambdas, it is possible to implement a sort of
> `static if` (see also N3613):
>
> template< typename T >
> void assign ( T& x, T const& y ) {
> x = y;
>
> static_if<boost::has_equal_to<T>::value>(
> std::bind([] ( auto x, auto y ) {
> assert(x == y);
> std::cout << "asserted for: " << typeid(T).name() << std::endl;
> }, x, y)
> ).else_(
> [] ( ) {
> std::cout << "cannot assert for: " << typeid(T).name() <<
> std::endl;
> }
> );
> }
>
> The bind around the generic lambda is a bit verbose... but this code
> compiles, asserts when called on int, and does not assert when called
> on x_t (tested on clang 3.4.2):
>
I had similar idea, but my solution was like:
```c++
template< typename T >
void assign ( T& x, T const& y ) {
x = y;
if_<boost::has_equal_to<T>::value>
(
[=](auto...) // make it lazy
{
assert(x == y);
std::cout << "asserted for: " << typeid(T).name() << std::endl;
}
, []
{
std::cout << "cannot assert for: " << typeid(T).name() <<
std::endl;
}
);
}
```
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.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk