My Project
Functions
forward.hpp File Reference
#include "remove_reference.hpp"

Go to the source code of this file.

Functions

template<typename T >
constexpr T && bsl::forward (bsl::remove_reference_t< T > &val) noexcept
 When "val" is a forwarding reference (a function argument that is declared as an rvalue reference to a cv-unqualified function template parameter), this overload forwards the argument to another function with the value category it had when passed to the calling function. More...
 
template<typename T >
constexpr T && bsl::forward (bsl::remove_reference_t< T > &&val) noexcept
 When "val" is a forwarding reference (a function argument that is declared as an rvalue reference to a cv-unqualified function template parameter), this overload forwards the argument to another function with the value category it had when passed to the calling function. More...
 

Detailed Description

Function Documentation

◆ forward() [1/2]

template<typename T >
constexpr T&& bsl::forward ( bsl::remove_reference_t< T > &  val)
noexcept

When "val" is a forwarding reference (a function argument that is declared as an rvalue reference to a cv-unqualified function template parameter), this overload forwards the argument to another function with the value category it had when passed to the calling function.

#include <bsl/forward.hpp>
#include <bsl/discard.hpp>
#include <bsl/move.hpp>
#include <bsl/debug.hpp>
namespace bsl
{
template<typename T>
constexpr bool
detector(T &&t) noexcept
{
return bsl::is_lvalue_reference<decltype(t)>::value;
}
template<typename T>
constexpr bool
forwarder(T &&t) noexcept
{
return detector(bsl::forward<T>(t));
}
inline void
example_forward_overview() noexcept
{
bsl::int32 const val1{42};
bsl::int32 val2{val1};
if (forwarder(val1)) {
bsl::print() << "success\n";
}
if (!forwarder(bsl::move(val2))) {
bsl::print() << "success\n";
}
}
}

SUPPRESSION: PRQA 4624 - false positive

  • We suppress this because A7-5-1 states that a function shall not return a pointer or reference to a parameter that is a const reference, and this is a false positive because this is the required definition for std::forward
Template Parameters
Tthe type that defines the value being forwarded
Parameters
valthe value being forwarded
Returns
static_cast<T &&>(val)

◆ forward() [2/2]

template<typename T >
constexpr T&& bsl::forward ( bsl::remove_reference_t< T > &&  val)
noexcept

When "val" is a forwarding reference (a function argument that is declared as an rvalue reference to a cv-unqualified function template parameter), this overload forwards the argument to another function with the value category it had when passed to the calling function.

#include <bsl/forward.hpp>
#include <bsl/discard.hpp>
#include <bsl/move.hpp>
#include <bsl/debug.hpp>
namespace bsl
{
template<typename T>
constexpr bool
detector(T &&t) noexcept
{
return bsl::is_lvalue_reference<decltype(t)>::value;
}
template<typename T>
constexpr bool
forwarder(T &&t) noexcept
{
return detector(bsl::forward<T>(t));
}
inline void
example_forward_overview() noexcept
{
bsl::int32 const val1{42};
bsl::int32 val2{val1};
if (forwarder(val1)) {
bsl::print() << "success\n";
}
if (!forwarder(bsl::move(val2))) {
bsl::print() << "success\n";
}
}
}

SUPPRESSION: PRQA 4624 - false positive

  • We suppress this because A7-5-1 states that a function shall not return a pointer or reference to a parameter that is a const reference, and this is a false positive because this is the required definition for std::forward
Template Parameters
Tthe type that defines the value being forwarded
Parameters
valthe value being forwarded
Returns
static_cast<T &&>(val)