diff --git a/doc/release_history.html b/doc/release_history.html
index 6e80372..c7744ec 100644
--- a/doc/release_history.html
+++ b/doc/release_history.html
@@ -45,6 +45,7 @@
As was announced in 1.84.0, Windows versions prior to 10 are no longer supported.
On Windows, canonical
is now based on the GetFinalPathNameByHandleW
WinAPI function. As a side effect, drive letters are converted to upper case, which makes the resulting paths more interoperable. (#325)
v4: canonical
no longer produces a trailing directory separator in the resulting path, if the input path has one.
+ If a path
constructor or member function is called with an argument of a user-defined type that is convertible to path
and one or more Source
types, the conversion to path
is now chosen by default. This may resolve argument conversion ambiguities in some cases, but may also result in a less optimal conversion path. If a different conversion path is desired, users are recommended to use explicit type casts. (#326)
1.86.0
diff --git a/include/boost/filesystem/detail/path_traits.hpp b/include/boost/filesystem/detail/path_traits.hpp
index 416c073..4cd81a2 100644
--- a/include/boost/filesystem/detail/path_traits.hpp
+++ b/include/boost/filesystem/detail/path_traits.hpp
@@ -25,6 +25,7 @@
#include
#include
#include
+#include
#include
#if defined(BOOST_FILESYSTEM_DETAIL_CXX23_STRING_VIEW_HAS_IMPLICIT_RANGE_CTOR)
#include
@@ -48,6 +49,7 @@ namespace filesystem {
BOOST_FILESYSTEM_DECL system::error_category const& codecvt_error_category() noexcept;
+class path;
class directory_entry;
namespace detail {
@@ -501,12 +503,30 @@ no_type check_convertible(...);
} // namespace is_convertible_to_path_source_impl
-//! The type trait indicates whether the type has a conversion path to one of the path source types
+template< typename T >
+struct check_is_convertible_to_path_source :
+ public std::integral_constant<
+ bool,
+ sizeof(is_convertible_to_path_source_impl::check_convertible(std::declval< T const& >())) == sizeof(yes_type)
+ >
+{
+};
+
+/*!
+ * \brief The type trait indicates whether the type has a conversion path to one of the path source types.
+ *
+ * \note The type trait returns `false` if the type is convertible to `path`. This prevents testing other
+ * conversion paths and forces the conversion to `path` to be chosen instead, to invoke a non-template
+ * member of `path` accepting a `path` argument.
+ */
template< typename T >
struct is_convertible_to_path_source :
public std::integral_constant<
bool,
- sizeof(is_convertible_to_path_source_impl::check_convertible(std::declval< T const& >())) == sizeof(yes_type)
+ detail::conjunction<
+ detail::negation< std::is_convertible< T, path > >,
+ check_is_convertible_to_path_source< T >
+ >::value
>
{
};
@@ -529,7 +549,7 @@ no_type check_convertible(...);
} // namespace is_convertible_to_std_string_view_impl
template< typename T >
-struct is_convertible_to_std_string_view :
+struct check_is_convertible_to_std_string_view :
public std::integral_constant<
bool,
sizeof(is_convertible_to_std_string_view_impl::check_convertible(std::declval< T const& >())) == sizeof(yes_type)
@@ -553,7 +573,7 @@ no_type check_convertible(...);
} // namespace is_convertible_to_path_source_non_std_string_view_impl
template< typename T >
-struct is_convertible_to_path_source_non_std_string_view :
+struct check_is_convertible_to_path_source_non_std_string_view :
public std::integral_constant<
bool,
sizeof(is_convertible_to_path_source_non_std_string_view_impl::check_convertible(std::declval< T const& >())) == sizeof(yes_type)
@@ -561,14 +581,23 @@ struct is_convertible_to_path_source_non_std_string_view :
{
};
-//! The type trait indicates whether the type has a conversion path to one of the path source types
+/*!
+ * \brief The type trait indicates whether the type has a conversion path to one of the path source types.
+ *
+ * \note The type trait returns `false` if the type is convertible to `path`. This prevents testing other
+ * conversion paths and forces the conversion to `path` to be chosen instead, to invoke a non-template
+ * member of `path` accepting a `path` argument.
+ */
template< typename T >
struct is_convertible_to_path_source :
public std::integral_constant<
bool,
- detail::disjunction<
- is_convertible_to_std_string_view< T >,
- is_convertible_to_path_source_non_std_string_view< T >
+ detail::conjunction<
+ detail::negation< std::is_convertible< T, path > >,
+ detail::disjunction<
+ check_is_convertible_to_std_string_view< T >,
+ check_is_convertible_to_path_source_non_std_string_view< T >
+ >
>::value
>
{
@@ -704,7 +733,7 @@ BOOST_FORCEINLINE typename Callback::result_type dispatch_convertible_sv_impl(st
template< typename Source, typename Callback >
BOOST_FORCEINLINE typename std::enable_if<
- !is_convertible_to_std_string_view< typename std::remove_cv< Source >::type >::value,
+ !check_is_convertible_to_std_string_view< typename std::remove_cv< Source >::type >::value,
typename Callback::result_type
>::type dispatch_convertible(Source const& source, Callback cb, const codecvt_type* cvt = nullptr)
{
@@ -714,7 +743,7 @@ BOOST_FORCEINLINE typename std::enable_if<
template< typename Source, typename Callback >
BOOST_FORCEINLINE typename std::enable_if<
- is_convertible_to_std_string_view< typename std::remove_cv< Source >::type >::value,
+ check_is_convertible_to_std_string_view< typename std::remove_cv< Source >::type >::value,
typename Callback::result_type
>::type dispatch_convertible(Source const& source, Callback cb, const codecvt_type* cvt = nullptr)
{
diff --git a/test/path_test.cpp b/test/path_test.cpp
index 4ac419c..c51009b 100644
--- a/test/path_test.cpp
+++ b/test/path_test.cpp
@@ -136,6 +136,43 @@ public:
operator fs::path() const { return m_path; }
};
+//! Test type to verify that the conversion to path is preferred (https://github.com/boostorg/filesystem/issues/326)
+class convertible_to_path_and_strings
+{
+private:
+ fs::path m_path;
+
+public:
+ convertible_to_path_and_strings() {}
+ convertible_to_path_and_strings(convertible_to_path_and_strings const& that) : m_path(that.m_path) {}
+ template< typename T >
+ convertible_to_path_and_strings(T const& that) : m_path(that) {}
+
+ convertible_to_path_and_strings& operator= (convertible_to_path_and_strings const& that)
+ {
+ m_path = that.m_path;
+ return *this;
+ }
+ template< typename T >
+ convertible_to_path_and_strings& operator= (T const& that)
+ {
+ m_path = that;
+ return *this;
+ }
+
+ operator fs::path() const { return m_path; }
+ operator const fs::path::value_type*() const
+ {
+#if defined(BOOST_WINDOWS_API)
+ return L"[invalid path]";
+#else
+ return "[invalid path]";
+#endif
+ }
+ operator fs::path::string_type() const { return fs::path::string_type(static_cast< const fs::path::value_type* >(*this)); }
+};
+
+
template< typename Char >
class basic_custom_string
{
@@ -2188,6 +2225,7 @@ void construction_tests()
PATH_TEST_EQ(derived_from_path("foo"), "foo");
PATH_TEST_EQ(convertible_to_path("foo"), "foo");
+ PATH_TEST_EQ(convertible_to_path_and_strings("foo"), "foo");
PATH_TEST_EQ(fs::path(pcustom_string("foo")), "foo");
PATH_TEST_EQ(boost::string_view("foo"), "foo");
#if !defined(BOOST_NO_CXX17_HDR_STRING_VIEW)
@@ -2204,9 +2242,9 @@ void construction_tests()
#if !defined(BOOST_NO_CXX17_HDR_STRING_VIEW)
#define APPEND_TEST_STD_STRING_VIEW(appnd, expected)\
- path p6(p);\
- p6 /= std::string_view(appnd);\
- PATH_TEST_EQ(p6, expected);
+ path p7(p);\
+ p7 /= std::string_view(appnd);\
+ PATH_TEST_EQ(p7, expected);
#else
#define APPEND_TEST_STD_STRING_VIEW(appnd, expected)
#endif
@@ -2229,15 +2267,18 @@ void construction_tests()
p3 /= convertible_to_path(appnd);\
PATH_TEST_EQ(p3, expected);\
path p4(p);\
- p4 /= pcustom_string(appnd);\
+ p4 /= convertible_to_path_and_strings(appnd);\
PATH_TEST_EQ(p4, expected);\
path p5(p);\
- p5 /= boost::string_view(appnd);\
+ p5 /= pcustom_string(appnd);\
PATH_TEST_EQ(p5, expected);\
+ path p6(p);\
+ p6 /= boost::string_view(appnd);\
+ PATH_TEST_EQ(p6, expected);\
APPEND_TEST_STD_STRING_VIEW(appnd, expected)\
- path p7(p);\
- p7.append(s.begin(), s.end());\
- PATH_TEST_EQ(p7.string(), expected);\
+ path p8(p);\
+ p8.append(s.begin(), s.end());\
+ PATH_TEST_EQ(p8.string(), expected);\
}
void append_tests()
@@ -2350,9 +2391,9 @@ void append_tests()
#if !defined(BOOST_NO_CXX17_HDR_STRING_VIEW)
#define CONCAT_TEST_STD_STRING_VIEW(appnd, expected)\
- path p9(p);\
- p9 += std::string_view(appnd);\
- PATH_TEST_EQ(p9, expected);
+ path p10(p);\
+ p10 += std::string_view(appnd);\
+ PATH_TEST_EQ(p10, expected);
#else
#define CONCAT_TEST_STD_STRING_VIEW(appnd, expected)
#endif
@@ -2380,15 +2421,18 @@ void append_tests()
p6 += convertible_to_path(appnd);\
PATH_TEST_EQ(p6, expected);\
path p7(p);\
- p7 += pcustom_string(appnd);\
+ p7 += convertible_to_path_and_strings(appnd);\
PATH_TEST_EQ(p7, expected);\
path p8(p);\
- p8 += boost::string_view(appnd);\
+ p8 += pcustom_string(appnd);\
PATH_TEST_EQ(p8, expected);\
+ path p9(p);\
+ p9 += boost::string_view(appnd);\
+ PATH_TEST_EQ(p9, expected);\
CONCAT_TEST_STD_STRING_VIEW(appnd, expected)\
- path p10(p);\
- p10.concat(s.begin(), s.end());\
- PATH_TEST_EQ(p10.string(), expected);\
+ path p11(p);\
+ p11.concat(s.begin(), s.end());\
+ PATH_TEST_EQ(p11.string(), expected);\
}
void concat_tests()