utl/include/utl/meta/sfinae.h
2019-01-29 16:01:02 +02:00

86 lines
2.4 KiB
C++

/*!
* \file sfinae.h
* \brief Template meta-programming SFINAE helpers
*
* Copyright (C) 2018 Christos Choutouridis
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __utl_meta_sfinae_h__
#define __utl_meta_sfinae_h__
#include <utl/core/impl.h>
/*!
* \ingroup meta
* \defgroup sfinae
* conditional use support header.
*/
//! @{
namespace utl {
namespace meta {
//! Tool to enable a partial specialization only if a boolean condition is true.
//! @{
namespace detail {
template <typename... T>
struct dev_null { using type = dev_null; }; //< Same as typelist
template <bool If> struct when_ { };
template <> struct when_<true> { using type = void; };
}
//! Well formed only if \p If is true
template <bool If>
using when = type_<detail::when_<If>>;
//! Well formed only if all of \p Ifs are \c true
template <bool ...Ifs>
using when_all = detail::dev_null<
when<Ifs>...
>;
//! @}
//! select _Tp if \p If is true, else SFINAE
//! We implement eneble_if so we don't have to pull entire \c <type_traits> from stl
//! @{
template <bool If, typename _Tp = void>
struct enable_if {
using type = _Tp;
};
template<typename _Tp>
struct enable_if <false, _Tp> { /* SFINAE*/ };
//! Alias template for enable_if
template <bool If, typename _Tp = void>
using use_if = enable_if<If, _Tp>;
//! Publicly recognized alias template for enable_if
template<bool If, typename _Tp = void>
using enable_if_t = type_<
enable_if<If, _Tp>
>;
//! Uniform alias template for use_if
template<bool If, typename _Tp = void>
using use_if_t = type_<
enable_if<If, _Tp>
>;
//! @}
}}
//! @}
#endif /* __utl_meta_sfinae_h__ */