|
BDE 4.14.0 Production release
|
Provide low-level functions on bslmf types.
This component defines a utility struct, bslmf::Util, that serves as a namespace for a suite of functions that supply low-level functionality for implementing portable generic facilities such as might be found in the C++ standard library.
The function forward emulates the C++ standard utility function std::forward with the addition that on compilers that don't support r-value references (i.e., C++03) a bslmf::MovableRef<t_T> is forwarded as a bslmf::MovableRef<t_T>. This operation is typically used via BSLS_COMPILERFEATURES_FORWARD (along with BSLS_COMPILERFEATURES_FORWARD_REF) when forwarding arguments in a generic context. See {Usage}.
The function forwardAsReference, like forward, emulates the C++ standard utility function std::forward with the difference that on compilers that don't support r-value references (C++03) a bslmf::MovableRef<t_T> is forwarded as const t_T& (instead of bslmf::MovableRef<t_T>). This operation is intended to be used when forwarding a MovableRef<t_T> where that MovableRef is being supplied to a function that does not support move emulation, but will support true C++11 r-value references (e.g., bdlf::BindUtil::bind).
The function moveIfSupported emulates the C++ standard utility function std::move with the addition that on compilers that don't support r-value references (i.e., C++03) an l-value reference is returned instead. This operation is intended to be used when moving an object to a function that does not support move emulation, but will support true C++11 r-value references (e.g., bdlf::BindUtil::bind).
This section illustrates intended use of this component.
Clients should generally not use bslmf::Util::forward directly, instead it should be used via BSLS_COMPILERFEATURES_FORWARD in conjunction with BSLS_COMPILERFEATURES_FORWARD_REF. Here we show a simple function using BSLS_COMPILERFEATURES_FORWARD:
Notice that bslmf::Util::forward is only used in conjunction with BSLS_COMPILERFEATURES_FORWARD_REF because, in the example above, if the create function's parameter type was ARG_TYPE&& then it is a C++11-only(!) forwarding reference, and we would simply use the standard std::forward. Alternatively, if the parameter type was MovableRef<ARG_TYPE> then arg is not a forwarding-reference to be forwarded (certainly not in C++03).
Suppose we have a class S1 that has a regular copy constructor, and only if the compiler supports rvalue references has to move constructor. We want to construct it with the move constructor if moves are supported and as a copy otherwise. Then we use bslmf::Util::forwardAsReference:
Suppose we had a function that takes a non-const lvalue-ref, and only when the compiler supports rvalue references also has an overload that takes rvalue references: