From 2dc22e7aad197236479b5e84240c0120073fd59e Mon Sep 17 00:00:00 2001 From: Rong Ou Date: Fri, 3 Mar 2023 08:24:13 -0800 Subject: [PATCH] Take advantage of C++17 features (#8858) --------- Co-authored-by: Hyunsu Philip Cho Co-authored-by: Jiaming Yuan --- R-package/src/Makevars.in | 2 +- R-package/src/Makevars.win | 2 +- src/common/common.h | 25 ------------------------- src/learner.cc | 12 ++++++------ 4 files changed, 8 insertions(+), 33 deletions(-) diff --git a/R-package/src/Makevars.in b/R-package/src/Makevars.in index 5cbfd0684..ed3f10571 100644 --- a/R-package/src/Makevars.in +++ b/R-package/src/Makevars.in @@ -3,7 +3,7 @@ PKGROOT=../../ ENABLE_STD_THREAD=1 # _*_ mode: Makefile; _*_ -CXX_STD = CXX14 +CXX_STD = CXX17 XGB_RFLAGS = -DXGBOOST_STRICT_R_MODE=1 -DDMLC_LOG_BEFORE_THROW=0\ -DDMLC_ENABLE_STD_THREAD=$(ENABLE_STD_THREAD) -DDMLC_DISABLE_STDIN=1\ diff --git a/R-package/src/Makevars.win b/R-package/src/Makevars.win index e8e1579f7..024ba1aa1 100644 --- a/R-package/src/Makevars.win +++ b/R-package/src/Makevars.win @@ -3,7 +3,7 @@ PKGROOT=../../ ENABLE_STD_THREAD=0 # _*_ mode: Makefile; _*_ -CXX_STD = CXX14 +CXX_STD = CXX17 XGB_RFLAGS = -DXGBOOST_STRICT_R_MODE=1 -DDMLC_LOG_BEFORE_THROW=0\ -DDMLC_ENABLE_STD_THREAD=$(ENABLE_STD_THREAD) -DDMLC_DISABLE_STDIN=1\ diff --git a/src/common/common.h b/src/common/common.h index 6bc96dfc9..35c807bef 100644 --- a/src/common/common.h +++ b/src/common/common.h @@ -195,31 +195,6 @@ template XGBOOST_DEVICE size_t LastOf(size_t group, Indexable const &indptr) { return indptr[group + 1] - 1; } - -/** - * \brief A CRTP (curiously recurring template pattern) helper function. - * - * https://www.fluentcpp.com/2017/05/19/crtp-helper/ - * - * Does two things: - * 1. Makes "crtp" explicit in the inheritance structure of a CRTP base class. - * 2. Avoids having to `static_cast` in a lot of places. - * - * \tparam T The derived class in a CRTP hierarchy. - */ -template -struct Crtp { - T &Underlying() { return static_cast(*this); } - T const &Underlying() const { return static_cast(*this); } -}; - -/** - * \brief C++17 std::as_const - */ -template -typename std::add_const::type &AsConst(T &v) noexcept { // NOLINT(runtime/references) - return v; -} } // namespace common } // namespace xgboost #endif // XGBOOST_COMMON_COMMON_H_ diff --git a/src/learner.cc b/src/learner.cc index dfcab281d..0e47c694c 100644 --- a/src/learner.cc +++ b/src/learner.cc @@ -21,7 +21,7 @@ #include #include #include -#include +#include // for as_const #include #include "collective/communicator-inl.h" @@ -257,11 +257,11 @@ LearnerModelParam::LearnerModelParam(Context const* ctx, LearnerModelParamLegacy : LearnerModelParam{user_param, t} { std::swap(base_score_, base_margin); // Make sure read access everywhere for thread-safe prediction. - common::AsConst(base_score_).HostView(); + std::as_const(base_score_).HostView(); if (!ctx->IsCPU()) { - common::AsConst(base_score_).View(ctx->gpu_id); + std::as_const(base_score_).View(ctx->gpu_id); } - CHECK(common::AsConst(base_score_).Data()->HostCanRead()); + CHECK(std::as_const(base_score_).Data()->HostCanRead()); } linalg::TensorView LearnerModelParam::BaseScore(int32_t device) const { @@ -287,9 +287,9 @@ void LearnerModelParam::Copy(LearnerModelParam const& that) { base_score_.Reshape(that.base_score_.Shape()); base_score_.Data()->SetDevice(that.base_score_.DeviceIdx()); base_score_.Data()->Copy(*that.base_score_.Data()); - common::AsConst(base_score_).HostView(); + std::as_const(base_score_).HostView(); if (that.base_score_.DeviceIdx() != Context::kCpuId) { - common::AsConst(base_score_).View(that.base_score_.DeviceIdx()); + std::as_const(base_score_).View(that.base_score_.DeviceIdx()); } CHECK_EQ(base_score_.Data()->DeviceCanRead(), that.base_score_.Data()->DeviceCanRead()); CHECK(base_score_.Data()->HostCanRead());