std::plus in C++ with examples
In this article, we will learn about std::plus in C++ with examples. It is a function object of the C++ standard library for performing addition. The object class whose call returns the result of adding its two arguments (as returned by operator + ).
std::plus in C++ with examples
It is defined in <functional> header.
template< class T > struct plus; (until C++14) template< class T = void > struct plus; (since C++14)
Specializations:
The standard library provides a specialization of std::plus
when T is not mentioned, which leaves the parameter types and return type to be deduced.
<plus><void>-which implement sum and return type.
Member types and definition:
- result_type-T
- first_argument_type-T
- second_argument_type -T
Member functions:
operator()
which returns the sum of two arguments.
std::plus::operator()
T operator()( const T& lhs, const T& rhs ) const; (until C++14) constexpr T operator()( const T& lhs, const T& rhs ) const; (since C++14)
It returns the sum of lhs and rhs.
The possible implementation of this function-
constexpr T operator()(const T &lhs, const T &rhs) const { return lhs + rhs; }
Example:
#include<iostream> #include<functional> using namespace std; int main(){ cout<<plus<>{}(5,9); }
Output:14
Leave a Reply