2016-11-10 7 views
1

バリデーショナルなテンプレートを試してみましたが、何らかの操作で特性値を最終的な値に一種減らすのが便利な点がありました。私にとってのユースケースは、variadicテンプレートパラメータの特性値を1つの値に減らすにはどうすればよいですか?

constexpr bool and(bool lhs, bool rhs){return lhs && rhs;} 

struct Foo 
{ 
    template< 
     typename ...Ts> 
    Foo(
     Ts&&... args) 
      noexcept(TraitReduction<std::is_nothrow_move_constructible, and, Ts...>::value) 
    {/*...*/} 
} 

です。問題は、STLの特性がすべて単一のテンプレート型であることです。 私の現在の作業ソリューションです:STLは、そのタスクsolutionforいくつかの標準化された(おそらくより便利な)を与える場合

template< 
    template<typename> class TraitT, 
    bool (*Operator)(bool,bool), 
    typename T1, 
    typename ...Ts> 
struct TraitReduction 
{ 
    static bool const value = TraitT<T1>::value; 
}; 

template< 
    template<typename> class TraitT, 
    bool (*Operator)(bool,bool), 
    typename T1, 
    typename T2, 
    typename ...Ts> 
struct TraitReduction< 
    TraitT, 
    Operator, 
    T1, 
    T2, 
    Ts...> 
{ 
    static bool const value = (*Operator)(
     TraitT<T1>::value, 
     TraitReduction<TraitT, Operator, T2, Ts...>::value); 
}; 

私の質問はありますか?もちろん、私の現在の解決策に関するコメントや、何が悪いのか、何が良いのかについては、ここで嬉しく思います。

+0

'noexcept()(is_nothrow_move_constructible {}と...) ' – 0x499602D2

+0

この特殊なケースは[' std :: connections'](http://en.cppreference.com/w/cpp/types/conjunction)の仕事のように見えますが、もっと一般化されたアプローチを探しているのなら、それは十分ではありません – krzaq

+0

'と'は '&&'の別のスペルですよね? –

答えて

3

あなたのソリューションは、あなたがより少ないインスタンスでそれを行う(まだ短絡なし)もインスタンス化の項の線形(および短絡の利点なし)

ある

template <bool...> struct bools{}; 

template <template <typename> Trait, typename ... Ts> 
struct all_of : std::is_same<bools<true, Trait<Ts>::value...>, 
          bools<Trait<Ts>::value..., true>> {}; 

あなたは可能線形ですが短絡しているstd::conjunctionを使用してください。

C++ 1Zおよび折りたたみ式、素敵な構文を有する(インスタンス化が、短絡することなく、())以下のインスタンスを有する。

(Trait<Ts>::value && ...) 
+0

最初と最後の位置に「真」を付ける興味深いトリックです。 – Orient

+0

2つのタイプの比較(長いテンプレート引数リストを持つクラステンプレートの特殊化)は* template-names *と* type-ids *(それらは* template-ids *ではない)と 'std ::結合はまた線形である。どの方が好きですか? – Orient

+0

'all_of'の定義に型エイリアスを使う方が良いかもしれません:' template < ... > all_of = typename std :: is_sameを使って< ... > :: type; ' – Orient

関連する問題