-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prevent default-construction of function pointers.
Default- and value-constructed pointers to functions are not callable, and therefore are not accepted as default-constructible condition function objects and deleters. Closes #14.
- Loading branch information
Showing
5 changed files
with
126 additions
and
7 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
include/boost/scope/detail/is_nonnull_default_constructible.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Distributed under the Boost Software License, Version 1.0. | ||
* (See accompanying file LICENSE_1_0.txt or copy at | ||
* https://www.boost.org/LICENSE_1_0.txt) | ||
* | ||
* Copyright (c) 2023 Andrey Semashev | ||
*/ | ||
/*! | ||
* \file scope/detail/is_nonnull_default_constructible.hpp | ||
* | ||
* This header contains definition of \c is_nonnull_default_constructible | ||
* and \c is_nothrow_nonnull_default_constructible type traits. The type | ||
* traits are useful for preventing default-construction of pointers to | ||
* functions where a default-constructed function object is expected. | ||
* Without it, default- or value-constructing a pointer to function would | ||
* produce a function object that is not callable. | ||
*/ | ||
|
||
#ifndef BOOST_SCOPE_DETAIL_IS_NONNULL_DEFAULT_CONSTRUCTIBLE_HPP_INCLUDED_ | ||
#define BOOST_SCOPE_DETAIL_IS_NONNULL_DEFAULT_CONSTRUCTIBLE_HPP_INCLUDED_ | ||
|
||
#include <type_traits> | ||
#include <boost/scope/detail/config.hpp> | ||
#include <boost/scope/detail/header.hpp> | ||
|
||
#ifdef BOOST_HAS_PRAGMA_ONCE | ||
#pragma once | ||
#endif | ||
|
||
namespace boost { | ||
namespace scope { | ||
namespace detail { | ||
|
||
//! The type trait checks if \c T is not a pointer and is default-constructible | ||
template< typename T > | ||
struct is_nonnull_default_constructible : | ||
public std::is_default_constructible< T > | ||
{ | ||
}; | ||
|
||
template< typename T > | ||
struct is_nonnull_default_constructible< T* > : | ||
public std::false_type | ||
{ | ||
}; | ||
|
||
//! The type trait checks if \c T is not a pointer and is nothrow-default-constructible | ||
template< typename T > | ||
struct is_nothrow_nonnull_default_constructible : | ||
public std::is_nothrow_default_constructible< T > | ||
{ | ||
}; | ||
|
||
template< typename T > | ||
struct is_nothrow_nonnull_default_constructible< T* > : | ||
public std::false_type | ||
{ | ||
}; | ||
|
||
} // namespace detail | ||
} // namespace scope | ||
} // namespace boost | ||
|
||
#include <boost/scope/detail/footer.hpp> | ||
|
||
#endif // BOOST_SCOPE_DETAIL_IS_NONNULL_DEFAULT_CONSTRUCTIBLE_HPP_INCLUDED_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Distributed under the Boost Software License, Version 1.0. | ||
* (See accompanying file LICENSE_1_0.txt or copy at | ||
* https://www.boost.org/LICENSE_1_0.txt) | ||
* | ||
* Copyright (c) 2023 Andrey Semashev | ||
*/ | ||
/*! | ||
* \file scope_exit_cond_def_cted_fptr.cpp | ||
* \author Andrey Semashev | ||
* | ||
* \brief This file tests that \c scope_exit with a function pointer | ||
* condition function object cannot be default-constructed. | ||
*/ | ||
|
||
#include <boost/scope/scope_exit.hpp> | ||
#include "function_types.hpp" | ||
|
||
int main() | ||
{ | ||
int n = 0; | ||
boost::scope::scope_exit< normal_func, bool (*)() > guard{ normal_func(n) }; | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* Distributed under the Boost Software License, Version 1.0. | ||
* (See accompanying file LICENSE_1_0.txt or copy at | ||
* https://www.boost.org/LICENSE_1_0.txt) | ||
* | ||
* Copyright (c) 2023 Andrey Semashev | ||
*/ | ||
/*! | ||
* \file unique_resource_del_def_cted_fptr.cpp | ||
* \author Andrey Semashev | ||
* | ||
* \brief This file tests that \c unique_resource with a function pointer | ||
* deleter cannot be default-constructed. | ||
*/ | ||
|
||
#include <boost/scope/unique_resource.hpp> | ||
|
||
int main() | ||
{ | ||
boost::scope::unique_resource< int, void (*)(int) > ur; | ||
|
||
return 0; | ||
} |