From 00f02167e35861bd11b8a9d6585444e9ef0b5172 Mon Sep 17 00:00:00 2001 From: Marshall Clow Date: Mon, 13 Feb 2017 10:25:04 -0800 Subject: [PATCH] Add tests to ensure that string_view|ref from rvalue fails (whenever it can) --- include/boost/utility/string_ref.hpp | 2 +- include/boost/utility/string_view.hpp | 13 ++++++------- test/Jamfile.v2 | 2 ++ test/string_ref_from_rvalue.cpp | 28 +++++++++++++++++++++++++++ test/string_view_from_rvalue.cpp | 28 +++++++++++++++++++++++++++ 5 files changed, 65 insertions(+), 8 deletions(-) create mode 100644 test/string_ref_from_rvalue.cpp create mode 100644 test/string_view_from_rvalue.cpp diff --git a/include/boost/utility/string_ref.hpp b/include/boost/utility/string_ref.hpp index ea47f76..96ab439 100644 --- a/include/boost/utility/string_ref.hpp +++ b/include/boost/utility/string_ref.hpp @@ -95,7 +95,7 @@ namespace boost { #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_NO_CXX11_DELETED_FUNCTIONS) // Constructing a string_ref from a temporary string is a bad idea template - basic_string_ref(std::basic_string&& str) + basic_string_ref( std::basic_string&&) = delete; #endif diff --git a/include/boost/utility/string_view.hpp b/include/boost/utility/string_view.hpp index 935dcb6..c48a515 100644 --- a/include/boost/utility/string_view.hpp +++ b/include/boost/utility/string_view.hpp @@ -91,15 +91,14 @@ namespace boost { #endif template - basic_string_view(const std::basic_string& str) BOOST_NOEXCEPT - : ptr_(str.data()), len_(str.length()) {} + basic_string_view(const std::basic_string& str) BOOST_NOEXCEPT + : ptr_(str.data()), len_(str.length()) {} #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_NO_CXX11_DELETED_FUNCTIONS) - // Constructing a string_view from a temporary string is a bad idea - template - basic_string_view(std::basic_string&& str) - = delete; + // Constructing a string_view from a temporary string is a bad idea + template + basic_string_view( std::basic_string&&) + = delete; #endif BOOST_CONSTEXPR basic_string_view(const charT* str) diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 7ca9efa..f211b62 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -27,9 +27,11 @@ test-suite utility [ run ../operators_test.cpp ../../test/build//boost_test_exec_monitor/static ] [ compile result_of_test.cpp ] [ run ../shared_iterator_test.cpp ] + [ compile-fail string_ref_from_rvalue.cpp ] [ run string_ref_test1.cpp unit_test_framework ] [ run string_ref_test2.cpp unit_test_framework ] [ run string_ref_test_io.cpp unit_test_framework ] + [ compile-fail string_view_from_rvalue.cpp ] [ run string_view_test1.cpp unit_test_framework ] [ run string_view_test2.cpp unit_test_framework ] [ run string_view_test_io.cpp unit_test_framework ] diff --git a/test/string_ref_from_rvalue.cpp b/test/string_ref_from_rvalue.cpp new file mode 100644 index 0000000..a109f17 --- /dev/null +++ b/test/string_ref_from_rvalue.cpp @@ -0,0 +1,28 @@ +/* + Copyright (c) Marshall Clow 2017. + + 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) + + For more information, see http://www.boost.org +*/ + +#include +#include +#include + +#include + +#if defined(BOOST_NO_CXX11_RVALUE_REFERENCES) || defined(BOOST_NO_CXX11_DELETED_FUNCTIONS) +#error "Unsupported test" +#endif + +#include "boost/test/minimal.hpp" + +std::string makeatemp() { return "abc"; } + +int test_main(int, char **) +{ + boost::basic_string_ref sv(makeatemp()); + return 0; +} diff --git a/test/string_view_from_rvalue.cpp b/test/string_view_from_rvalue.cpp new file mode 100644 index 0000000..3402a08 --- /dev/null +++ b/test/string_view_from_rvalue.cpp @@ -0,0 +1,28 @@ +/* + Copyright (c) Marshall Clow 2017. + + 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) + + For more information, see http://www.boost.org +*/ + +#include +#include +#include + +#include + +#if defined(BOOST_NO_CXX11_RVALUE_REFERENCES) || defined(BOOST_NO_CXX11_DELETED_FUNCTIONS) +#error "Unsupported test" +#endif + +#include "boost/test/minimal.hpp" + +std::string makeatemp() { return "abc"; } + +int test_main(int, char **) +{ + boost::basic_string_view sv(makeatemp()); + return 0; +}