Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions base_from_member_ref_test.cpp
Original file line number Diff line number Diff line change
@@ -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();
}
13 changes: 13 additions & 0 deletions doc/base_from_member.qbk
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
13 changes: 13 additions & 0 deletions include/boost/utility/base_from_member.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,19 @@ class 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


Expand Down
1 change: 1 addition & 0 deletions test/Jamfile.v2
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]
Expand Down