美文网首页
boost::mpl::contains std::enabl

boost::mpl::contains std::enabl

作者: zxlele_c763 | 来源:发表于2021-07-23 12:59 被阅读0次

MPL 库

  • MPL(Meta-Programming Library)是由David Abrahams和Aleksey Gurtovoy为方便模板元编程而开发的库,2003年被Boost吸纳为其中的一员,此后又历经一些大幅度修改,已经相当完善,其最新版本于2004年11月发布。MPL的出现是C++模板元编程发展中的一大创举,它提供了一个通用、高层次的编程框架,其中包括了序列(Sequence)、迭代器(Iterator)、算法(Algorithm)、元函数(Metafunction)等多种组件,具有高度的可重用性,不但提高了模板元编程的效率,而且使模板元编程的应用范围得到相当的扩展

mpl introduce

std::enable_if_t

std::enable_if_t

std::enable_if_t

  • template<bool Cond, class T = void> struct enable_if {};
  • template<class T> struct enable_if<true, T> { typedef T type; };
  • template< bool B, class T = void >
  • using enable_if_t = typename enable_if<B,T>::type;

define std::enable_if

Enable type if condition is met
The type T is enabled as member type enable_if::type if Cond is true.

Otherwise, enable_if::type is not defined.

This is useful to hide signatures on compile time when a particular condition is not met,
since in this case, the member enable_if::type will not be defined and attempting to compile > using it should fail.

It is defined with a behavior equivalent to:

template<bool Cond, class T = void> struct enable_if {};
template<class T> struct enable_if<true, T> { typedef T type; };

C++11模板元编程



C++11中引入了std::enable_if函数,函数原型如下:

template< bool B, class T = void >
struct enable_if;

可能的函数实现:

template<bool B, class T = void>
struct enable_if {};

template<class T>
struct enable_if<true, T> { typedef T type; };

由上可知,只有当第一个模板参数为true时,enable_if会包含一个type=T的公有成员,否则没有该公有成员。

头文件:
1

include <type_traits>

std::enable_if使用场景

1、限制模板函数的参数类型

在某些场景下,我们需要实现只有特定类型可以调用的模板函数。如下代码所示,通过对返回值使用
std::enable_if和在模板参数中使用std::enable_if均实现了只允许整形参数调用函数的功能。

// enable_if example: two ways of using enable_if
#include <iostream>
#include <type_traits>
 
// 1. the return type (bool) is only valid if T is an integral type:
template <class T>
typename std::enable_if<std::is_integral<T>::value,bool>::type
 is_odd (T i) {return bool(i%2);}
 
// 2. the second template argument is only valid if T is an integral type:
template < class T,
      class = typename std::enable_if<std::is_integral<T>::value>::type>
bool is_even (T i) {return !bool(i%2);}
 
int main() {
 
 short int i = 1;  // code does not compile if type of i is not integral
 
 std::cout << std::boolalpha;
 std::cout << "i is odd: " << is_odd(i) << std::endl;
 std::cout << "i is even: " << is_even(i) << std::endl;
 
 return 0;
}

当使用float类型参数调用函数时,程序会报错:

    error: no matching function for call to 'is_odd(float&)'

2. 模板类型偏特化

在使用模板编程时,可以利用std::enable_if的特性根据模板参数的不同特性进行不同的类型选择。
如下所示,我们可以实现一个检测变量是否为智能指针的实现:

#include <iostream>
#include <type_traits>
#include <memory>
 
template <typename T>
struct is_smart_pointer_helper : public std::false_type {};
 
template <typename T>
struct is_smart_pointer_helper<std::shared_ptr<T> > : public std::true_type {};
 
template <typename T>
struct is_smart_pointer_helper<std::unique_ptr<T> > : public std::true_type {};
 
template <typename T>
struct is_smart_pointer_helper<std::weak_ptr<T> > : public std::true_type {};
 
template <typename T>
struct is_smart_pointer : public is_smart_pointer_helper<typename std::remove_cv<T>::type> {};
 
template <typename T>
typename std::enable_if<is_smart_pointer<T>::value,void>::type check_smart_pointer(const T& t)
{
  std::cout << "is smart pointer" << std::endl;
}
 
template <typename T>
typename std::enable_if<!is_smart_pointer<T>::value,void>::type check_smart_pointer(const T& t)
{
  std::cout << "not smart pointer" << std::endl;
}
 
int main()
{
  int* p(new int(2));
  std::shared_ptr<int> pp(new int(2));
  std::unique_ptr<int> upp(new int(4));
 
  check_smart_pointer(p);
  check_smart_pointer(pp);
  check_smart_pointer(upp);
   
  return 0;
}

程序输出:

    not smart pointer
    is smart pointer
    is smart pointer

// call
boost::apply_visitor(queueEventVisitor, queuedEvent);

// define
using enable_if_t = typename std::enable_if<E, T>::type;

using Class1EventRegisterSA = boost::mpl::list<
messages::ngap::PduSessionResourceSetupReqWithAmfId,
messages::ngap::PduSessionResourceModifyReqWithAmfId,
messages::internal::UeContextModificationReqFallback,
messages::internal::UeContextModificationReqNgcFlex,
messages::internal::UeContextModificationReqWithOnlyIndexToRFSP>;

using ParallelProcedureQueueManagerSA =
ParallelProcedureQueueManager<IUeSa, EventlistSA, Class1EventRegisterSA, QueuedEventHandlerSA>;

class QueueEventVisitor : public boost::static_visitor<>
{
public:
explicit QueueEventVisitor(UeType& ue) : ue{ue} {}

    template <typename T, std::enable_if_t<boost::mpl::contains<Class1EventRegister, T>::value, bool> = true>
    void operator()(const T& msg) const
    {
        constexpr auto msgName = getTypeName<T>(); // get type name during compiletime
        LOG_DEBUG_MSG("Reject {} for UE {}", msgName, ue.getUeIds());
        ue.reject(msg);
    }

    template <typename T, std::enable_if_t<not boost::mpl::contains<Class1EventRegister, T>::value, bool> = true>
    void operator()(const T&) const
    {
    }

private:
    UeType& ue;
};










相关文章

网友评论

      本文标题:boost::mpl::contains std::enabl

      本文链接:https://www.haomeiwen.com/subject/gqbumltx.html