2015-09-11 16 views
7

Örneğintürünü std::is_pointer<T> ve std::is_const<T> ise kullanmak istiyorum.C++ type_traits standart yolundan koşulların nasıl birleştirileceği

template <typename T> 
void f(T t, std::true_type, std::true_type) {} 
template <typename T> 
void f(T t) 
{ 
    f(t, std::is_pointer<T>{}, std::is_const<T>{}); 
} 

Ama hiç böyle bir şey istiyorum: Tabii

ve basit bir yöntemdir böyle olduğunu

template <typename T> 
void f(T t, std::true_type) {} 
template <typename T> 
void f(T t) 
{ 
    f(t, std::and<std::is_pointer<T>, std::is_const<T>>{}); 
} 

c mi ++ std::and gibi standart sınıflar şey? Eğer hayır, istenen işlevselliği ile uygulamak için basit bir yoldur?

std::integral_constant<bool, 
         std::is_pointer<T>::value && std::is_const<T>::value> 

Yoksa genel bir özellik and yazabilirsiniz:

cevap

9

Sen özelliklerin sadece && araya sonuçları ve onları bir std::integral_constant koyabilirsiniz. here bazı olasılıklar:

Seçenek 1:

template<typename... Conds> 
    struct and_ 
    : std::true_type 
    { }; 

template<typename Cond, typename... Conds> 
    struct and_<Cond, Conds...> 
    : std::conditional<Cond::value, and_<Conds...>, std::false_type>::type 
    { }; 

//usage 
and_<std::is_pointer<T>, std::is_const<T>> 

Seçenek 2: Biz fold expressions olsun

template<bool...> struct bool_pack; 
template<bool... bs> 
using and_ = std::is_same<bool_pack<bs..., true>, bool_pack<true, bs...>>; 

//usage 
and_<std::is_pointer<T>, std::is_const<T>> 

bunu yapmak mümkün olacak:

Derleyiciniz bunu this gibi -std=c++1z bayrağı altında destekleyebilir.

template <class T, template <class> class... Ps> 
constexpr bool satisfies_all_v = std::conjunction<Ps<T>...>::value; 

template <class T, template <class> class... Ps> 
constexpr bool satisfies_any_v = std::disjunction<Ps<T>...>::value; 

Ve bu Kullanmaya şey mi: kolayca variadic (sayısı) için beste olabilir C++ 17 conjunction ve disjunction gelişiyle birlikte

4

yüklemler

satisfies_all_v<T, is_pointer, is_const> 

Demo

İlgili konular