diff --git a/include/boost/optional/detail/optional_config.hpp b/include/boost/optional/detail/optional_config.hpp index c069c0d..4c1e539 100644 --- a/include/boost/optional/detail/optional_config.hpp +++ b/include/boost/optional/detail/optional_config.hpp @@ -52,4 +52,11 @@ # define BOOST_OPTIONAL_DETAIL_USE_ATTRIBUTE_MAY_ALIAS #endif +#if (defined(_MSC_VER) && _MSC_VER <= 1800) +// on MSCV 2013 and earlier an unwanted temporary is created when you assign from +// a const lvalue of integral type. Thus we bind not to the original address but +// to a temporary. +# define BOOST_OPTIONAL_CONFIG_NO_PROPER_ASSIGN_FROM_CONST_INT +#endif + #endif // header guard diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 6b4e857..f526563 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -60,5 +60,7 @@ import testing ; [ compile-fail optional_test_fail_io_without_io.cpp ] [ compile-fail optional_test_fail_none_io_without_io.cpp ] [ compile-fail optional_test_fail_convert_assign_of_enums.cpp ] + [ run optional_xconfig_NO_PROPER_ASSIGN_FROM_CONST_INT_pass.cpp ] + [ run-fail optional_xconfig_NO_PROPER_ASSIGN_FROM_CONST_INT_fail.cpp ] ; } diff --git a/test/optional_xconfig_NO_PROPER_ASSIGN_FROM_CONST_INT_fail.cpp b/test/optional_xconfig_NO_PROPER_ASSIGN_FROM_CONST_INT_fail.cpp new file mode 100644 index 0000000..7bf6d92 --- /dev/null +++ b/test/optional_xconfig_NO_PROPER_ASSIGN_FROM_CONST_INT_fail.cpp @@ -0,0 +1,42 @@ +// Copyright (C) 2015 Andrzej Krzemienski. +// +// Use, modification, and distribution is subject to 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) +// +// See http://www.boost.org/lib/optional for documentation. +// +// You are welcome to contact the author at: +// akrzemi1@gmail.com + +#include "boost/core/lightweight_test.hpp" +#include "boost/optional/detail/optional_config.hpp" + +#ifndef BOOST_OPTIONAL_CONFIG_NO_PROPER_ASSIGN_FROM_CONST_INT + +int main() +{ + BOOST_ERROR("failed as requested"); + return boost::report_errors(); +} + +#else + +const int global_i = 0; + +struct Binder +{ + void operator=(const int& i) + { + BOOST_TEST(&i == &global_i); + } +}; + +int main() +{ + Binder s; + s = global_i; + return boost::report_errors(); +} + +#endif diff --git a/test/optional_xconfig_NO_PROPER_ASSIGN_FROM_CONST_INT_pass.cpp b/test/optional_xconfig_NO_PROPER_ASSIGN_FROM_CONST_INT_pass.cpp new file mode 100644 index 0000000..942b856 --- /dev/null +++ b/test/optional_xconfig_NO_PROPER_ASSIGN_FROM_CONST_INT_pass.cpp @@ -0,0 +1,41 @@ +// Copyright (C) 2015 Andrzej Krzemienski. +// +// Use, modification, and distribution is subject to 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) +// +// See http://www.boost.org/lib/optional for documentation. +// +// You are welcome to contact the author at: +// akrzemi1@gmail.com + +#include "boost/core/lightweight_test.hpp" +#include "boost/optional/detail/optional_config.hpp" + +#ifndef BOOST_OPTIONAL_CONFIG_NO_PROPER_ASSIGN_FROM_CONST_INT + +const int global_i = 0; + +struct Binder +{ + void operator=(const int& i) + { + BOOST_TEST(&i == &global_i); + } +}; + +int main() +{ + Binder s; + s = global_i; + return boost::report_errors(); +} + +#else + +int main() +{ + return boost::report_errors(); +} + +#endif diff --git a/test/optional_xconfig_NO_PROPER_CONVERT_FROM_CONST_INT_fail.cpp b/test/optional_xconfig_NO_PROPER_CONVERT_FROM_CONST_INT_fail.cpp new file mode 100644 index 0000000..ae16261 --- /dev/null +++ b/test/optional_xconfig_NO_PROPER_CONVERT_FROM_CONST_INT_fail.cpp @@ -0,0 +1,41 @@ +// Copyright (C) 2015 Andrzej Krzemienski. +// +// Use, modification, and distribution is subject to 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) +// +// See http://www.boost.org/lib/optional for documentation. +// +// You are welcome to contact the author at: +// akrzemi1@gmail.com + +#include "boost/core/lightweight_test.hpp" +#include "boost/optional/detail/optional_config.hpp" + +#ifndef BOOST_OPTIONAL_CONFIG_NO_PROPER_ASSIGN_FROM_CONST_INT + +int main() +{ + BOOST_ERROR("failed as requested"); + return boost::report_errors(); +} + +#else + +const int global_i = 0; + +struct Binder +{ + Binder(const int& i) + { + BOOST_TEST(&i == &global_i); + } +}; + +int main() +{ + Binder b = global_i; + return boost::report_errors(); +} + +#endif diff --git a/test/optional_xconfig_NO_PROPER_CONVERT_FROM_CONST_INT_pass.cpp b/test/optional_xconfig_NO_PROPER_CONVERT_FROM_CONST_INT_pass.cpp new file mode 100644 index 0000000..ea1f3ce --- /dev/null +++ b/test/optional_xconfig_NO_PROPER_CONVERT_FROM_CONST_INT_pass.cpp @@ -0,0 +1,40 @@ +// Copyright (C) 2015 Andrzej Krzemienski. +// +// Use, modification, and distribution is subject to 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) +// +// See http://www.boost.org/lib/optional for documentation. +// +// You are welcome to contact the author at: +// akrzemi1@gmail.com + +#include "boost/core/lightweight_test.hpp" +#include "boost/optional/detail/optional_config.hpp" + +#ifndef BOOST_OPTIONAL_CONFIG_NO_PROPER_ASSIGN_FROM_CONST_INT + +const int global_i = 0; + +struct Binder +{ + Binder(const int& i) + { + BOOST_TEST(&i == &global_i); + } +}; + +int main() +{ + Binder b = global_i; + return boost::report_errors(); +} + +#else + +int main() +{ + return boost::report_errors(); +} + +#endif