From 5d5f42f99b7409b6b051182fae69fd22f8877a6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ho=C5=99e=C5=88ovsk=C3=BD?= Date: Tue, 26 Dec 2023 11:57:38 +0100 Subject: [PATCH] Enable moving Opts into Parser This also required keeping the reference type in the fluent interface for Opts. Removes another ~40 allocations. --- src/catch2/internal/catch_clara.hpp | 30 ++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/src/catch2/internal/catch_clara.hpp b/src/catch2/internal/catch_clara.hpp index a9413810..4bcef406 100644 --- a/src/catch2/internal/catch_clara.hpp +++ b/src/catch2/internal/catch_clara.hpp @@ -495,10 +495,14 @@ namespace Catch { m_ref( std::make_shared>( ref ) ), m_hint( hint ) {} - auto operator()( StringRef description ) -> DerivedT& { + DerivedT& operator()( StringRef description ) & { m_description = description; return static_cast( *this ); } + DerivedT&& operator()( StringRef description ) && { + m_description = description; + return static_cast( *this ); + } auto optional() -> DerivedT& { m_optionality = Optionality::Optional; @@ -567,10 +571,14 @@ namespace Catch { Opt( T& ref, StringRef hint ): ParserRefImpl( ref, hint ) {} - auto operator[](std::string const& optName) -> Opt& { + Opt& operator[]( std::string const& optName ) & { m_optNames.push_back(optName); return *this; } + Opt&& operator[]( std::string const& optName ) && { + m_optNames.push_back( optName ); + return CATCH_MOVE(*this); + } Detail::HelpColumns getHelpColumns() const; @@ -628,21 +636,25 @@ namespace Catch { return *this; } - auto operator|=(Opt const& opt) -> Parser& { - m_options.push_back(opt); - return *this; + friend Parser& operator|=( Parser& p, Opt const& opt ) { + p.m_options.push_back( opt ); + return p; + } + friend Parser& operator|=( Parser& p, Opt&& opt ) { + p.m_options.push_back( CATCH_MOVE(opt) ); + return p; } Parser& operator|=(Parser const& other); template - friend Parser operator|(Parser const& p, T const& rhs) { - return Parser( p ) |= rhs; + friend Parser operator|( Parser const& p, T&& rhs ) { + return Parser( p ) |= CATCH_FORWARD(rhs); } template - friend Parser operator|( Parser&& p, T const& rhs ) { - p |= rhs; + friend Parser operator|( Parser&& p, T&& rhs ) { + p |= CATCH_FORWARD(rhs); return CATCH_MOVE(p); }