Making `std::get` play nice with SFINAE

后端 未结 4 633
南笙
南笙 2021-02-05 10:20

std::get does not seem to be SFINAE-friendly, as shown by the following test case:

template 
auto foo(C &c) -> declty         


        
4条回答
  •  难免孤独
    2021-02-05 11:08

    Don't SFINAE on std::get; that is not permitted.

    Here are two relatively sfinae friendly ways to test if you can using std::get; get(t):

    template
    using can_get=std::integral_constant::value>;
    
    namespace helper{
      template
      struct can_get_type:std::false_type{};
      template
      struct can_get_type>:
        std::integral_constant+...)==1>
      {};
    }
    template
    using can_get=typename helpers::can_get_type::type;
    

    Then your code reads:

    template {},int> =0>
    decltype(auto) foo(C &c) {
      return std::get(c);
    }
    

提交回复
热议问题