Strange MSVC behaviour with std::experimental::is_detected

后端 未结 2 2030
囚心锁ツ
囚心锁ツ 2021-01-14 23:02

I implemented std::experimental::is_detected based on this article on cppreference.com (Part of the code is below + working repro).

It works well on G++

2条回答
  •  离开以前
    2021-01-14 23:37

    Here's a workaround that appears to work with recent MSVC (tested with Visual C++ 19.00.23720.0):

    #include 
    
    template 
    using void_t = void;
    
    namespace internal
    {
        template 
        struct detect_impl
        {
            using value_t = V;
            using type = D;
        };
    
        template  class Check, typename... Args>
        auto detect_check(char)
            -> detect_impl;
    
        template  class Check, typename... Args>
        auto detect_check(int)
            -> decltype(void_t>(),
                        detect_impl>{});
    
        template  class Check, typename... Args>
        struct detect : decltype(detect_check(0)) {};
    }
    
    struct nonesuch
    {
        nonesuch() = delete;
        ~nonesuch() = delete;
        nonesuch(nonesuch const&) = delete;
        void operator=(nonesuch const&) = delete;
    };
    
    template  class Check, typename... Args>
    using is_detected = typename internal::detect::value_t;
    

    (The dummy void parameter is unused now, it's there just to keep the rest of the implementation intact.)

提交回复
热议问题