Type trait to check that all types in a parameter pack are copy constructible

后端 未结 4 507
生来不讨喜
生来不讨喜 2020-12-08 04:36

I need a type trait to check whether all types in a parameter pack are copy constructible. This is what I\'ve done so far. The main function contains some test cases, to che

4条回答
  •  难免孤独
    2020-12-08 05:07

    First define a reusable utility to test whether every predicate in a pack is true:

    template
      struct and_
      : std::true_type
      { };
    
    template
      struct and_
      : std::conditional, std::false_type>::type
      { };
    

    Then it's trivial to use that with is_copy_constructible (or any other unary type trait):

    template
      using areCopyConstructible = and_...>;
    

    One advantage of defining and_ like this is that it short-circuits, i.e. stops instantiating is_copy_constructible for the rest of the pack after the first false result.

提交回复
热议问题