mirror of
https://github.com/boostorg/utility.git
synced 2025-05-09 15:04:00 +00:00
Merge pull request #12 from K-ballo/base-from-ref-member
Added base_from_member specialization for members of lvalue-reference types.
This commit is contained in:
commit
f1edd107eb
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
|
#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 class template has a first template parameter `MemberType` representing
|
||||||
the type of the based-member. It has a last template parameter `UniqueID`,
|
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
|
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
|
arguments (currently up to ten) as possible and pass them to a constructor
|
||||||
of the data member.
|
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
|
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
|
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.
|
as possible to the actual type used in the data member's desired constructor.
|
||||||
|
@ -148,6 +148,19 @@ protected:
|
|||||||
|
|
||||||
}; // boost::base_from_member
|
}; // 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
|
} // namespace boost
|
||||||
|
|
||||||
|
|
||||||
|
@ -21,6 +21,7 @@ test-suite utility
|
|||||||
[ run ../addressof_test.cpp ]
|
[ run ../addressof_test.cpp ]
|
||||||
[ run ../addressof_test2.cpp ]
|
[ run ../addressof_test2.cpp ]
|
||||||
[ run ../base_from_member_test.cpp ]
|
[ run ../base_from_member_test.cpp ]
|
||||||
|
[ run ../base_from_member_ref_test.cpp ]
|
||||||
[ run ../binary_test.cpp ]
|
[ run ../binary_test.cpp ]
|
||||||
[ run ../call_traits_test.cpp : -u ]
|
[ run ../call_traits_test.cpp : -u ]
|
||||||
[ compile-fail ../checked_delete_test.cpp ]
|
[ compile-fail ../checked_delete_test.cpp ]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user