From 7f9fd2bd0eda11ecf1acd65fb79da3965de7df1e Mon Sep 17 00:00:00 2001 From: Filip Sajdak Date: Tue, 4 Oct 2022 22:22:17 +0200 Subject: [PATCH] Refactor CPP2_UFCS macro to work also with no args Current implementation have two macros `CPP2_UFCS` and `CPP2_UFCS_0`. One for functions/methods with arguments and the other for no arguments case. There is special macro `__VA_OPT__()` that includes its argument only if `__VA_ARGS__` has more then zero arguments. In this change `CPP2_UFCS_0` is no longer needed and is removed. `CPP2_UFCS` works for both cases. --- include/cpp2util.h | 19 +++++-------------- source/cppfront.cpp | 8 +------- 2 files changed, 6 insertions(+), 21 deletions(-) diff --git a/include/cpp2util.h b/include/cpp2util.h index e6c0edff13..045597a7f6 100644 --- a/include/cpp2util.h +++ b/include/cpp2util.h @@ -459,22 +459,13 @@ class out { //----------------------------------------------------------------------- // #define CPP2_UFCS(FUNCNAME,PARAM1,...) \ -[](auto&& obj, auto&& ...params) { \ - if constexpr (requires{ std::forward(obj).FUNCNAME(std::forward(params)...); }) { \ - return std::forward(obj).FUNCNAME(std::forward(params)...); \ +[](auto&& obj __VA_OPT__(, auto&& ...params)) { \ + if constexpr (requires{ std::forward(obj).FUNCNAME( __VA_OPT__(std::forward(params)...) ); }) { \ + return std::forward(obj).FUNCNAME( __VA_OPT__(std::forward(params)...) ); \ } else { \ - return FUNCNAME(std::forward(obj), std::forward(params)...); \ + return FUNCNAME(std::forward(obj) __VA_OPT__(, std::forward(params)...) ); \ } \ -}(PARAM1, __VA_ARGS__) - -#define CPP2_UFCS_0(FUNCNAME,PARAM1) \ -[](auto&& obj) { \ - if constexpr (requires{ std::forward(obj).FUNCNAME(); }) { \ - return std::forward(obj).FUNCNAME(); \ - } else { \ - return FUNCNAME(std::forward(obj)); \ - } \ -}(PARAM1) +}(PARAM1 __VA_OPT__(, __VA_ARGS__)) //----------------------------------------------------------------------- diff --git a/source/cppfront.cpp b/source/cppfront.cpp index 36149d9200..bbade8c28d 100644 --- a/source/cppfront.cpp +++ b/source/cppfront.cpp @@ -1624,13 +1624,7 @@ class cppfront // The ( has its expr_list and op_close assert (n.ops[1].expr_list && n.ops[1].op_close); - // If there are no additional arguments, use the CPP2_UFCS_0 version - if (!n.ops[1].expr_list->expressions.empty()) { - printer.print_cpp2("CPP2_UFCS(", n.position()); - } - else { - printer.print_cpp2("CPP2_UFCS_0(", n.position()); - } + printer.print_cpp2("CPP2_UFCS(", n.position()); // Make the "funcname" the first argument to CPP2_UFCS emit(*n.ops[0].id_expr);