Move fclose_deleter and null_deleter to their own namespaces to block ADL.

This prevents bringing namespace boost into ADL when the deleters are used
in template parameters, e.g. in std::unique_ptr.
This commit is contained in:
Andrey Semashev
2024-02-02 05:12:41 +03:00
parent 7b1d3718c1
commit 95f0b35c36
6 changed files with 88 additions and 0 deletions

View File

@ -11,6 +11,15 @@
* Added a new [link core.functor `boost/core/functor.hpp`] header with a `functor` class template
for wrapping a raw function into a function object class.
* Changed [link core.null_deleter `null_deleter`] and [link core.fclose_deleter `fclose_deleter`]
definitions so that they don't bring namespace `boost` into argument-dependent lookup in cases
like this:
```
std::unique_ptr< std::FILE, boost::fclose_deleter > p1, p2;
swap(p1, p2); // no longer looks for boost::swap as part of ADL
```
Users may need to either explicitly qualify the namespace of the called function or add a
`using`-declaration.
[endsect]

View File

@ -26,6 +26,9 @@
namespace boost {
// Block unintended ADL
namespace fclose_deleter_ns {
//! A function object that closes a file
struct fclose_deleter
{
@ -41,6 +44,10 @@ struct fclose_deleter
}
};
} // namespace fclose_deleter_ns
using fclose_deleter_ns::fclose_deleter;
} // namespace boost
#endif // BOOST_CORE_FCLOSE_DELETER_HPP

View File

@ -27,6 +27,9 @@
namespace boost {
// Block unintended ADL
namespace null_deleter_ns {
//! A function object that does nothing and can be used as an empty deleter for \c shared_ptr
struct null_deleter
{
@ -39,6 +42,10 @@ struct null_deleter
void operator() (T*) const BOOST_NOEXCEPT {}
};
} // namespace null_deleter_ns
using null_deleter_ns::null_deleter;
} // namespace boost
#endif // BOOST_CORE_NULL_DELETER_HPP

View File

@ -187,7 +187,12 @@ compile-fail scoped_enum_compile_fail_conv_to_int.cpp
run underlying_type.cpp ;
compile-fail null_deleter_compile_fail_adl.cpp
: $(warnings-as-errors-off) ;
run fclose_deleter_test.cpp : : : <target-os>windows:<define>_CRT_SECURE_NO_WARNINGS <target-os>windows:<define>_CRT_SECURE_NO_DEPRECATE ;
compile-fail fclose_deleter_compile_fail_adl.cpp
: <target-os>windows:<define>_CRT_SECURE_NO_WARNINGS <target-os>windows:<define>_CRT_SECURE_NO_DEPRECATE $(warnings-as-errors-off) ;
run functor_test.cpp ;

View File

@ -0,0 +1,30 @@
/*
* Copyright Andrey Semashev 2024.
* 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)
*/
/*!
* \file fclose_deleter_compile_fail_adl.cpp
* \author Andrey Semashev
* \date 02.02.2024
*
* This file tests that \c boost::fclose_deleter doesn't bring namespace
* boost into ADL.
*/
#include <boost/core/fclose_deleter.hpp>
namespace boost {
void check_adl(fclose_deleter const&)
{
}
} // namespace boost
int main()
{
// Must not find boost::check_adl
check_adl(boost::fclose_deleter());
}

View File

@ -0,0 +1,30 @@
/*
* Copyright Andrey Semashev 2024.
* 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)
*/
/*!
* \file fclose_deleter_compile_fail_adl.cpp
* \author Andrey Semashev
* \date 02.02.2024
*
* This file tests that \c boost::null_deleter doesn't bring namespace
* boost into ADL.
*/
#include <boost/core/null_deleter.hpp>
namespace boost {
void check_adl(null_deleter const&)
{
}
} // namespace boost
int main()
{
// Must not find boost::check_adl
check_adl(boost::null_deleter());
}