mirror of
https://github.com/boostorg/functional.git
synced 2025-06-25 03:51:35 +02:00
Move identity to core
This commit is contained in:
@ -1,45 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
|
||||
<title>Boost Function Object Adapter Library</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Identity</h1>
|
||||
<p>The header <a href="../../boost/functional/identity.hpp">
|
||||
<boost/functional/identity.hpp></a> provides the function object
|
||||
<code>boost::identity</code> whose <code>operator()</code> returns its
|
||||
argument. It is an implementation of C++20's <code>std::identity</code>
|
||||
that supports C++03 and above.</p>
|
||||
<h2>Example</h2>
|
||||
<p>It is commonly used as the default projection in constrained algorithms.</p>
|
||||
<pre>
|
||||
<code>template<class Range, class Projection = boost::identity>
|
||||
void print(Range&& range, Projection projection = {});</code>
|
||||
</pre>
|
||||
<h2>Reference</h2>
|
||||
<pre>
|
||||
<code>namespace boost {
|
||||
|
||||
struct identity {
|
||||
using is_transparent = <em>unspecified</em>;
|
||||
|
||||
template<class T>
|
||||
T&& operator()(T&& value) const noexcept;
|
||||
};
|
||||
|
||||
} // boost</code>
|
||||
</pre>
|
||||
<h3>Operators</h3>
|
||||
<dl>
|
||||
<dt><code>template<class T>
|
||||
T&& operator()(T&& value) const noexcept;</code></dt>
|
||||
<dd>Returns <code>std::forward<T>(value)</code></dd>
|
||||
</dl>
|
||||
<hr>
|
||||
<p>Copyright 2021 Glen Joseph Fernandes.</p>
|
||||
<p>Distributed under the
|
||||
<a href="http://www.boost.org/LICENSE_1_0.txt">Boost Software License,
|
||||
Version 1.0</a>.</p>
|
||||
</body>
|
||||
</html>
|
@ -8,54 +8,10 @@ Distributed under the Boost Software License, Version 1.0.
|
||||
#ifndef BOOST_FUNCTIONAL_IDENTITY_HPP
|
||||
#define BOOST_FUNCTIONAL_IDENTITY_HPP
|
||||
|
||||
#include <boost/config.hpp>
|
||||
#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
|
||||
#include <utility>
|
||||
#endif
|
||||
|
||||
namespace boost {
|
||||
|
||||
struct identity {
|
||||
typedef void is_transparent;
|
||||
|
||||
#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
|
||||
template<class T>
|
||||
BOOST_CONSTEXPR T&& operator()(T&& value) const BOOST_NOEXCEPT {
|
||||
return std::forward<T>(value);
|
||||
}
|
||||
#else
|
||||
template<class T>
|
||||
BOOST_CONSTEXPR const T& operator()(const T& value) const BOOST_NOEXCEPT {
|
||||
return value;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
BOOST_CONSTEXPR T& operator()(T& value) const BOOST_NOEXCEPT {
|
||||
return value;
|
||||
}
|
||||
#endif
|
||||
|
||||
template<class>
|
||||
struct result { };
|
||||
|
||||
template<class T>
|
||||
struct result<identity(T&)> {
|
||||
typedef T& type;
|
||||
};
|
||||
|
||||
#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
|
||||
template<class T>
|
||||
struct result<identity(T)> {
|
||||
typedef T&& type;
|
||||
};
|
||||
|
||||
template<class T>
|
||||
struct result<identity(T&&)> {
|
||||
typedef T&& type;
|
||||
};
|
||||
#endif
|
||||
};
|
||||
|
||||
} // boost
|
||||
/*
|
||||
The header file at this path is deprecated;
|
||||
use <boost/core/identity.hpp> instead.
|
||||
*/
|
||||
#include <boost/core/identity.hpp>
|
||||
|
||||
#endif
|
||||
|
@ -121,12 +121,6 @@
|
||||
|
||||
<td valign="top">Based on section 20.3.8 of the standard.</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<th align="left"><a href="identity.html">Identity</a></th>
|
||||
<td valign="top"><tt>identity</tt></td>
|
||||
<td valign="top">[func.identity] in ISO/IEC 14882:2020</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<h3>Usage</h3>
|
||||
|
@ -6,8 +6,4 @@ import testing ;
|
||||
|
||||
test-suite functional :
|
||||
[ run function_test.cpp ]
|
||||
[ run identity_test.cpp ]
|
||||
[ run identity_rvalue_test.cpp ]
|
||||
[ run identity_result_of_test.cpp ]
|
||||
[ run identity_result_of_rvalue_test.cpp ]
|
||||
;
|
||||
|
@ -1,31 +0,0 @@
|
||||
/*
|
||||
Copyright 2022 Glen Joseph Fernandes
|
||||
(glenjofe@gmail.com)
|
||||
|
||||
Distributed under the Boost Software License, Version 1.0.
|
||||
(http://www.boost.org/LICENSE_1_0.txt)
|
||||
*/
|
||||
#include <boost/config.hpp>
|
||||
#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
|
||||
#include <boost/functional/identity.hpp>
|
||||
#include <boost/core/lightweight_test_trait.hpp>
|
||||
#include <boost/utility/result_of.hpp>
|
||||
|
||||
int main()
|
||||
{
|
||||
BOOST_TEST_TRAIT_SAME(boost::result_of<boost::identity(int)>::type,
|
||||
int&&);
|
||||
BOOST_TEST_TRAIT_SAME(boost::result_of<boost::identity(const int)>::type,
|
||||
int&&);
|
||||
BOOST_TEST_TRAIT_SAME(boost::result_of<boost::identity(int&&)>::type,
|
||||
int&&);
|
||||
BOOST_TEST_TRAIT_SAME(boost::result_of<boost::identity(const int&&)>::type,
|
||||
const int&&);
|
||||
return boost::report_errors();
|
||||
}
|
||||
#else
|
||||
int main()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
#endif
|
@ -1,18 +0,0 @@
|
||||
/*
|
||||
Copyright 2022 Glen Joseph Fernandes
|
||||
(glenjofe@gmail.com)
|
||||
|
||||
Distributed under the Boost Software License, Version 1.0.
|
||||
(http://www.boost.org/LICENSE_1_0.txt)
|
||||
*/
|
||||
#include <boost/functional/identity.hpp>
|
||||
#include <boost/core/lightweight_test_trait.hpp>
|
||||
#include <boost/utility/result_of.hpp>
|
||||
|
||||
int main()
|
||||
{
|
||||
BOOST_TEST_TRAIT_SAME(boost::result_of<boost::identity(int&)>::type, int&);
|
||||
BOOST_TEST_TRAIT_SAME(boost::result_of<boost::identity(const int&)>::type,
|
||||
const int&);
|
||||
return boost::report_errors();
|
||||
}
|
@ -1,75 +0,0 @@
|
||||
/*
|
||||
Copyright 2021 Glen Joseph Fernandes
|
||||
(glenjofe@gmail.com)
|
||||
|
||||
Distributed under the Boost Software License, Version 1.0.
|
||||
(http://www.boost.org/LICENSE_1_0.txt)
|
||||
*/
|
||||
#include <boost/config.hpp>
|
||||
#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
|
||||
#include <boost/functional/identity.hpp>
|
||||
#include <boost/core/lightweight_test.hpp>
|
||||
#include <algorithm>
|
||||
#include <iterator>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
enum kind {
|
||||
rvalue,
|
||||
crvalue,
|
||||
other
|
||||
};
|
||||
|
||||
kind result(std::string&&)
|
||||
{
|
||||
return rvalue;
|
||||
}
|
||||
|
||||
kind result(const std::string&&)
|
||||
{
|
||||
return crvalue;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
kind result(T&&)
|
||||
{
|
||||
return other;
|
||||
}
|
||||
|
||||
void simple_test()
|
||||
{
|
||||
typedef std::string string;
|
||||
BOOST_TEST(boost::identity()(string("a")) == string("a"));
|
||||
BOOST_TEST(result(boost::identity()(string("a"))) == rvalue);
|
||||
typedef const std::string cstring;
|
||||
BOOST_TEST(boost::identity()(cstring("a")) == cstring("a"));
|
||||
BOOST_TEST(result(boost::identity()(cstring("a"))) == crvalue);
|
||||
}
|
||||
|
||||
void algorithm_test()
|
||||
{
|
||||
std::vector<std::string> v1;
|
||||
v1.push_back(std::string("a"));
|
||||
v1.push_back(std::string("b"));
|
||||
v1.push_back(std::string("c"));
|
||||
std::vector<std::string> v2(v1);
|
||||
std::vector<std::string> v3;
|
||||
std::transform(std::make_move_iterator(v2.begin()),
|
||||
std::make_move_iterator(v2.end()),
|
||||
std::back_inserter(v3),
|
||||
boost::identity());
|
||||
BOOST_TEST(v3 == v1);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
simple_test();
|
||||
algorithm_test();
|
||||
return boost::report_errors();
|
||||
}
|
||||
#else
|
||||
int main()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
#endif
|
@ -1,72 +0,0 @@
|
||||
/*
|
||||
Copyright 2021 Glen Joseph Fernandes
|
||||
(glenjofe@gmail.com)
|
||||
|
||||
Distributed under the Boost Software License, Version 1.0.
|
||||
(http://www.boost.org/LICENSE_1_0.txt)
|
||||
*/
|
||||
#include <boost/functional/identity.hpp>
|
||||
#include <boost/core/lightweight_test.hpp>
|
||||
#include <algorithm>
|
||||
#include <iterator>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
enum kind {
|
||||
ref,
|
||||
cref,
|
||||
other
|
||||
};
|
||||
|
||||
kind result(std::string&)
|
||||
{
|
||||
return ref;
|
||||
}
|
||||
|
||||
kind result(const std::string&)
|
||||
{
|
||||
return cref;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
kind result(T&)
|
||||
{
|
||||
return other;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
kind result(const T&)
|
||||
{
|
||||
return other;
|
||||
}
|
||||
|
||||
void simple_test()
|
||||
{
|
||||
std::string s1("a");
|
||||
BOOST_TEST(boost::identity()(s1) == s1);
|
||||
BOOST_TEST(&boost::identity()(s1) == &s1);
|
||||
BOOST_TEST(result(boost::identity()(s1)) == ref);
|
||||
const std::string s2("a");
|
||||
BOOST_TEST(boost::identity()(s2) == s2);
|
||||
BOOST_TEST(&boost::identity()(s2) == &s2);
|
||||
BOOST_TEST(result(boost::identity()(s2)) == cref);
|
||||
}
|
||||
|
||||
void algorithm_test()
|
||||
{
|
||||
std::vector<std::string> v1;
|
||||
v1.push_back(std::string("a"));
|
||||
v1.push_back(std::string("b"));
|
||||
v1.push_back(std::string("c"));
|
||||
std::vector<std::string> v2;
|
||||
std::transform(v1.begin(), v1.end(), std::back_inserter(v2),
|
||||
boost::identity());
|
||||
BOOST_TEST(v2 == v1);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
simple_test();
|
||||
algorithm_test();
|
||||
return boost::report_errors();
|
||||
}
|
Reference in New Issue
Block a user