mirror of
https://github.com/boostorg/core.git
synced 2025-07-29 20:37:22 +02:00
Move identity from functional to core
This commit is contained in:
@ -55,6 +55,7 @@ criteria for inclusion is that the utility component be:
|
||||
[include exchange.qbk]
|
||||
[include explicit_operator_bool.qbk]
|
||||
[include first_scalar.qbk]
|
||||
[include identity.qbk]
|
||||
[include ignore_unused.qbk]
|
||||
[include is_same.qbk]
|
||||
[include launder.qbk]
|
||||
|
61
doc/identity.qbk
Normal file
61
doc/identity.qbk
Normal file
@ -0,0 +1,61 @@
|
||||
[/
|
||||
Copyright 2021-2023 Glen Joseph Fernandes
|
||||
(glenjofe@gmail.com)
|
||||
|
||||
Distributed under the Boost Software License, Version 1.0.
|
||||
(http://www.boost.org/LICENSE_1_0.txt)
|
||||
]
|
||||
|
||||
[section:identity identity]
|
||||
|
||||
[simplesect Authors]
|
||||
|
||||
* Glen Fernandes
|
||||
|
||||
[endsimplesect]
|
||||
|
||||
[section Overview]
|
||||
|
||||
The header <boost/functional/identity.hpp> provides the function object
|
||||
`boost::identity` whose `operator()` returns its argument. It is an
|
||||
implementation of C++20's `std::identity` that supports C++03 and above.
|
||||
|
||||
[endsect]
|
||||
|
||||
[section Example]
|
||||
|
||||
It is commonly used as the default projection in constrained algorithms.
|
||||
|
||||
```
|
||||
template<class Range, class Projection = boost::identity>
|
||||
void print(Range&& range, Projection projection = {});
|
||||
```
|
||||
|
||||
[endsect]
|
||||
|
||||
[section Reference]
|
||||
|
||||
```
|
||||
namespace boost {
|
||||
|
||||
struct identity {
|
||||
using is_transparent = unspecified;
|
||||
|
||||
template<class T>
|
||||
T&& operator()(T&& value) const noexcept;
|
||||
};
|
||||
|
||||
} /* boost */
|
||||
```
|
||||
|
||||
[section Operators]
|
||||
|
||||
[variablelist
|
||||
[[`template<class T> T&& operator()(T&& value) const noexcept;`]
|
||||
[Returns `std::forward<T>(value)`.]]]
|
||||
|
||||
[endsect]
|
||||
|
||||
[endsect]
|
||||
|
||||
[endsect]
|
61
include/boost/core/identity.hpp
Normal file
61
include/boost/core/identity.hpp
Normal file
@ -0,0 +1,61 @@
|
||||
/*
|
||||
Copyright 2021-2023 Glen Joseph Fernandes
|
||||
(glenjofe@gmail.com)
|
||||
|
||||
Distributed under the Boost Software License, Version 1.0.
|
||||
(http://www.boost.org/LICENSE_1_0.txt)
|
||||
*/
|
||||
#ifndef BOOST_CORE_IDENTITY_HPP
|
||||
#define BOOST_CORE_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 */
|
||||
|
||||
#endif
|
@ -368,5 +368,8 @@ run serialization_split_free_test.cpp : : : <library>/boost//serialization/<warn
|
||||
run serialization_split_member_test.cpp : : : <library>/boost//serialization/<warnings>off ;
|
||||
run serialization_construct_data_test.cpp : : : <library>/boost//serialization/<warnings>off ;
|
||||
|
||||
run identity_test.cpp ;
|
||||
run identity_rvalue_test.cpp ;
|
||||
|
||||
use-project /boost/core/swap : ./swap ;
|
||||
build-project ./swap ;
|
||||
|
69
test/identity_rvalue_test.cpp
Normal file
69
test/identity_rvalue_test.cpp
Normal file
@ -0,0 +1,69 @@
|
||||
/*
|
||||
Copyright 2021-2023 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/core/identity.hpp>
|
||||
#include <boost/core/lightweight_test.hpp>
|
||||
#include <algorithm>
|
||||
#include <iterator>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
bool test(std::string&&)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
bool test(const std::string&&)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
bool test(T&&)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
void simple_test()
|
||||
{
|
||||
typedef std::string string;
|
||||
BOOST_TEST(boost::identity()(string("a")) == string("a"));
|
||||
BOOST_TEST(test(boost::identity()(string("a"))));
|
||||
typedef const std::string cstring;
|
||||
BOOST_TEST(boost::identity()(cstring("a")) == cstring("a"));
|
||||
BOOST_TEST(test(boost::identity()(cstring("a"))));
|
||||
}
|
||||
|
||||
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
|
66
test/identity_test.cpp
Normal file
66
test/identity_test.cpp
Normal file
@ -0,0 +1,66 @@
|
||||
/*
|
||||
Copyright 2021-2023 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/core/identity.hpp>
|
||||
#include <boost/core/lightweight_test.hpp>
|
||||
#include <algorithm>
|
||||
#include <iterator>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
bool test(std::string&)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
bool test(const std::string&)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
bool test(T&)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
bool test(const T&)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
void simple_test()
|
||||
{
|
||||
std::string s1("a");
|
||||
BOOST_TEST(boost::identity()(s1) == s1);
|
||||
BOOST_TEST(&boost::identity()(s1) == &s1);
|
||||
BOOST_TEST(test(boost::identity()(s1)));
|
||||
const std::string s2("a");
|
||||
BOOST_TEST(boost::identity()(s2) == s2);
|
||||
BOOST_TEST(&boost::identity()(s2) == &s2);
|
||||
BOOST_TEST(test(boost::identity()(s2)));
|
||||
}
|
||||
|
||||
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