diff --git a/base_from_member_ref_test.cpp b/base_from_member_ref_test.cpp new file mode 100644 index 0000000..fc6f249 --- /dev/null +++ b/base_from_member_ref_test.cpp @@ -0,0 +1,29 @@ +// +// Test that a base_from_member 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 + +#include + +struct foo : boost::base_from_member +{ + explicit foo(int& ref) : boost::base_from_member(ref) + { + BOOST_TEST(&member == &ref); + } +}; + +int main() +{ + int i = 0; + foo f(i); + + return boost::report_errors(); +} diff --git a/doc/base_from_member.qbk b/doc/base_from_member.qbk index 0a4f568..9afd949 100644 --- a/doc/base_from_member.qbk +++ b/doc/base_from_member.qbk @@ -143,6 +143,16 @@ type does not need to concern itself with the integer. #endif }; + template < typename MemberType, int UniqueID > + class base_from_member + { + 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. diff --git a/include/boost/utility/base_from_member.hpp b/include/boost/utility/base_from_member.hpp index e32ecb8..fc0e13c 100644 --- a/include/boost/utility/base_from_member.hpp +++ b/include/boost/utility/base_from_member.hpp @@ -148,6 +148,19 @@ protected: }; // boost::base_from_member +template < typename MemberType, int UniqueID > +class base_from_member +{ +protected: + MemberType& member; + + explicit BOOST_CONSTEXPR base_from_member( MemberType& x ) + BOOST_NOEXCEPT + : member( x ) + {} + +}; // boost::base_from_member + } // namespace boost diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 3b7b9a1..2829574 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -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 ]