From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wm1-f65.google.com (mail-wm1-f65.google.com [209.85.128.65]) by sourceware.org (Postfix) with ESMTPS id 391553851C27 for ; Fri, 21 Aug 2020 14:45:35 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 391553851C27 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=palves.net Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=alves.ped@gmail.com Received: by mail-wm1-f65.google.com with SMTP id 9so2151661wmj.5 for ; Fri, 21 Aug 2020 07:45:35 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=WDlWeg/Fy6XvcpzC2m5SdCNaNET6xKZobqKxs4dtl3s=; b=OWjTjaa1YOiRZ7B86jdyrGkIeIePJRXq67gaiK7Lea8WIlNOYRJMjqSLKlNro9cI+Z mVDxz1VSzUpH5XUG9MUaKg4lN7Xp0KAyMaYYd5rYY6v2z4sRpErTKja13BFhO4XZDKGh imJHPbt+AqZydbjq3qhehCHnN6/nVilou0bhnqzLjVm/aeO/TXoav4Qj9XO1V1o9w/4G DQCtBlsTSDKHq4/Q8oF2Z+Zcod2sBuumRWY2aGMbU/VZFhPINJUYPWW2pxsikFOA3QbS 0Dp6FiOQWi14uDnoUDc8MadKA0MSvfHS3DXJ0HcOyX/V7jBBEfKWPU1vGYKJJC+uYKia Bb/g== X-Gm-Message-State: AOAM530v1sG/sDpXFJe/FaoOpmk8qdUOCe/Hl68bZ4rkXW6iV0o2Z60g bVds3ekUr+4xwUcX4UmaFp/NHeIbiVCaSg== X-Google-Smtp-Source: ABdhPJwOUODjeECe3KKd5n+FhRQpOPiJBroVZNgGMbkhLGSbnX4WLUFj0E+inW6+VsvEGyUctoi32g== X-Received: by 2002:a7b:ce12:: with SMTP id m18mr2938499wmc.3.1598021133380; Fri, 21 Aug 2020 07:45:33 -0700 (PDT) Received: from localhost ([2001:8a0:f905:5600:56ee:75ff:fe8d:232b]) by smtp.gmail.com with ESMTPSA id b203sm6738275wmc.22.2020.08.21.07.45.31 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Fri, 21 Aug 2020 07:45:32 -0700 (PDT) From: Pedro Alves To: gdb-patches@sourceware.org Subject: [PATCH 1/3] Rewrite valid-expr.h's internals in terms of the detection idiom (C++17/N4502) Date: Fri, 21 Aug 2020 15:45:21 +0100 Message-Id: <20200821144523.19451-2-pedro@palves.net> X-Mailer: git-send-email 2.14.5 In-Reply-To: <20200821144523.19451-1-pedro@palves.net> References: <20200821144523.19451-1-pedro@palves.net> X-Spam-Status: No, score=-8.5 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, KAM_DMARC_STATUS, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 21 Aug 2020 14:45:36 -0000 An earlier attempt at doing this had failed (wouldn't work in GCCs around 4.8, IIRC), but now that I try again, it works. I suspect that my previous attempt did not use the pre C++14-safe void_t (in traits.h). I want to switch to this model because: - It's the standard detection idiom that folks will learn starting with C++17. - In the enum_flags unit tests, I have a static_assert that triggers a warning (resulting in build error), which GCC does not suppress because the warning is not being triggered in the SFINAE context. Switching to the detection idiom fixes that. Alternatively, switching to the C++03-style expression-validity checking with a varargs overload would allow addressing that, but I think that would be going backwards idiomatically speaking. - While this patch shows a net increase of lines of code, the magic being added to traits.h can be removed in a few years when we start requiring C++17. gdbsupport/ChangeLog: * traits.h (struct nonesuch, struct detector, detected_or) (detected_or_t, is_detected, detected_t, detected_or) (detected_or_t, is_detected_exact, is_detected_convertible): New. * valid-expr.h (CHECK_VALID_EXPR_INT): Use gdb::is_detected_exact. --- gdbsupport/traits.h | 67 +++++++++++++++++++++++++++++++++++++++++++++++++ gdbsupport/valid-expr.h | 20 +++------------ 2 files changed, 70 insertions(+), 17 deletions(-) diff --git a/gdbsupport/traits.h b/gdbsupport/traits.h index 2a6f00654c..93b609ac10 100644 --- a/gdbsupport/traits.h +++ b/gdbsupport/traits.h @@ -52,6 +52,73 @@ struct make_void { typedef void type; }; template using void_t = typename make_void::type; +/* Implementation of the detection idiom: + + - http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4502.pdf + - http://en.cppreference.com/w/cpp/experimental/is_detected + +*/ + +struct nonesuch +{ + nonesuch () = delete; + ~nonesuch () = delete; + nonesuch (const nonesuch &) = delete; + void operator= (const nonesuch &) = delete; +}; + +namespace detection_detail { +/* Implementation of the detection idiom (negative case). */ +template class Op, typename... Args> +struct detector +{ + using value_t = std::false_type; + using type = Default; +}; + +/* Implementation of the detection idiom (positive case). */ +template class Op, typename... Args> +struct detector>, Op, Args...> +{ + using value_t = std::true_type; + using type = Op; +}; + +/* Detect whether Op is a valid type, use Default if not. */ +template class Op, + typename... Args> +using detected_or = detector; + +/* Op if that is a valid type, otherwise Default. */ +template class Op, + typename... Args> +using detected_or_t + = typename detected_or::type; + +} /* detection_detail */ + +template class Op, typename... Args> +using is_detected + = typename detection_detail::detector::value_t; + +template class Op, typename... Args> +using detected_t + = typename detection_detail::detector::type; + +template class Op, typename... Args> +using detected_or = detection_detail::detected_or; + +template class Op, typename... Args> +using detected_or_t = typename detected_or::type; + +template class Op, typename... Args> +using is_detected_exact = std::is_same>; + +template class Op, typename... Args> +using is_detected_convertible + = std::is_convertible, To>; + /* A few trait helpers, mainly stolen from libstdc++. Uppercase because "and/or", etc. are reserved keywords. */ diff --git a/gdbsupport/valid-expr.h b/gdbsupport/valid-expr.h index b1c8446814..a22fa61134 100644 --- a/gdbsupport/valid-expr.h +++ b/gdbsupport/valid-expr.h @@ -58,26 +58,12 @@ #define CHECK_VALID_EXPR_INT(TYPENAMES, TYPES, VALID, EXPR_TYPE, EXPR) \ namespace CONCAT (check_valid_expr, __LINE__) { \ \ - template \ - struct is_valid_expression \ - : std::false_type {}; \ - \ template \ - struct is_valid_expression> \ - : std::true_type {}; \ + using archetype = decltype (EXPR); \ \ - static_assert (is_valid_expression::value == VALID, \ + static_assert (gdb::is_detected_exact::value == VALID, \ ""); \ - \ - template \ - struct is_same_type \ - : std::is_same {}; \ - \ - template \ - struct is_same_type> \ - : std::is_same {}; \ - \ - static_assert (is_same_type::value, ""); \ } /* namespace */ /* A few convenience macros that support expressions involving a -- 2.14.5