mirror of
https://github.com/boostorg/utility.git
synced 2025-05-08 18:34:02 +00:00
Added base_from_member specialization for members of lvalue-reference type
This commit is contained in:
parent
61d07273fc
commit
51e482edfe
29
base_from_member_ref_test.cpp
Normal file
29
base_from_member_ref_test.cpp
Normal file
@ -0,0 +1,29 @@
|
||||
//
|
||||
// Test that a base_from_member<T&> can be properly constructed
|
||||
//
|
||||
// Copyright 2014 Agustin Berge
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
// See accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt
|
||||
//
|
||||
|
||||
#include <boost/utility/base_from_member.hpp>
|
||||
|
||||
#include <boost/detail/lightweight_test.hpp>
|
||||
|
||||
struct foo : boost::base_from_member<int&>
|
||||
{
|
||||
explicit foo(int& ref) : boost::base_from_member<int&>(ref)
|
||||
{
|
||||
BOOST_TEST(&member == &ref);
|
||||
}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
int i = 0;
|
||||
foo f(i);
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
@ -143,6 +143,16 @@ type does not need to concern itself with the integer.
|
||||
#endif
|
||||
};
|
||||
|
||||
template < typename MemberType, int UniqueID >
|
||||
class base_from_member<MemberType&, UniqueID>
|
||||
{
|
||||
protected:
|
||||
MemberType& member;
|
||||
|
||||
explicit constexpr base_from_member( MemberType& x )
|
||||
noexcept;
|
||||
};
|
||||
|
||||
The class template has a first template parameter `MemberType` representing
|
||||
the type of the based-member. It has a last template parameter `UniqueID`,
|
||||
that is an `int`, to differentiate between multiple base classes that use
|
||||
@ -169,6 +179,9 @@ constructor member templates. These constructor templates can take as many
|
||||
arguments (currently up to ten) as possible and pass them to a constructor
|
||||
of the data member.
|
||||
|
||||
A specialization for member references offers a single constructor taking
|
||||
a `MemberType&`, which is the only way to initialize a reference.
|
||||
|
||||
Since C++ does not allow any way to explicitly state the template parameters
|
||||
of a templated constructor, make sure that the arguments are already close
|
||||
as possible to the actual type used in the data member's desired constructor.
|
||||
|
@ -148,6 +148,19 @@ protected:
|
||||
|
||||
}; // boost::base_from_member
|
||||
|
||||
template < typename MemberType, int UniqueID >
|
||||
class base_from_member<MemberType&, UniqueID>
|
||||
{
|
||||
protected:
|
||||
MemberType& member;
|
||||
|
||||
explicit BOOST_CONSTEXPR base_from_member( MemberType& x )
|
||||
BOOST_NOEXCEPT
|
||||
: member( x )
|
||||
{}
|
||||
|
||||
}; // boost::base_from_member
|
||||
|
||||
} // namespace boost
|
||||
|
||||
|
||||
|
@ -21,6 +21,7 @@ test-suite utility
|
||||
[ run ../addressof_test.cpp ]
|
||||
[ run ../addressof_test2.cpp ]
|
||||
[ run ../base_from_member_test.cpp ]
|
||||
[ run ../base_from_member_ref_test.cpp ]
|
||||
[ run ../binary_test.cpp ]
|
||||
[ run ../call_traits_test.cpp : -u ]
|
||||
[ compile-fail ../checked_delete_test.cpp ]
|
||||
|
Loading…
x
Reference in New Issue
Block a user