[filesystem] basic_path as parameter

I'd like to use basic_path as a function parameter so that it works correctly whether a wide or narrow string is passed. template<typename T> void func(basic_path<T> file) { do thing to file } func("myfile"); func(L"myfile"); However I can't do this because basic_path has a second Traits template parameter which is not itself a template. So I tried the following workaround: template<typename T> struct traits_chooser; template<> struct traits_chooser<char*> { typedef boost::filesystem::path_traits traits; }; template<> struct traits_chooser<wchar_t*> { typedef boost::filesystem::wpath_traits traits; }; template<typename T> void func(basic_path<T, typename traits_chooser<T>::traits> file) {...} But my compiler (MSVC 2005) still can't deduce the parameter type. Does anyone know how I can get this to work? Many thanks. Alex Lamaison

AMDG Alex Lamaison wrote:
I'd like to use basic_path as a function parameter so that it works correctly whether a wide or narrow string is passed.
template<typename T> void func(basic_path<T> file) { do thing to file }
func("myfile"); func(L"myfile");
However I can't do this because basic_path has a second Traits template parameter which is not itself a template. So I tried the following workaround:
template<typename T> struct traits_chooser;
template<> struct traits_chooser<char*> { typedef boost::filesystem::path_traits traits; };
template<> struct traits_chooser<wchar_t*> { typedef boost::filesystem::wpath_traits traits; };
template<typename T> void func(basic_path<T, typename traits_chooser<T>::traits> file) {...}
But my compiler (MSVC 2005) still can't deduce the parameter type. Does anyone know how I can get this to work?
Try specializing for char and wchar_t instead of char* and wchar_t*. You could also just use template<typename T, typename Traits> void func(const basic_bath<T, Traits>& file); In Christ, Steven Watanabe

On Tue, 09 Mar 2010 09:38:32 -0800, Steven Watanabe wrote:
Alex Lamaison wrote:
I'd like to use basic_path as a function parameter so that it works correctly whether a wide or narrow string is passed.
template<typename T> void func(basic_path<T> file) { do thing to file }
func("myfile"); func(L"myfile");
Try specializing for char and wchar_t instead of char* and wchar_t*. You could also just use
template<typename T, typename Traits> void func(const basic_bath<T, Traits>& file);
Thanks Steven. I tried both of these but still no luck. For this definition: template<typename T, typename Traits> inline hmodule load_library( const boost::filesystem::basic_path<T, Traits>& library_path) {} called like this: load_library("kernel32.dll"); load_library(L"kernel32.dll"); I get this error (namespaces stripped): "error C2784: 'hmodule load_library(const basic_path<String,Traits> &)' : could not deduce template argument for 'const basic_path<String,Traits> &' from 'const char [13]'" and similar for wchar_t[13]. What can I do to help its powers of deduction? Alex P.S. Sorry for all the dupes; GMANE issues :(

AMDG Alex Lamaison wrote:
I'd like to use basic_path as a function parameter so that it works correctly whether a wide or narrow string is passed.
template<typename T> void func(basic_path<T> file) { do thing to file }
func("myfile"); func(L"myfile");
There's no way to get the compiler to deduce T, since the argument types are "const char[...]" and "const wchar_t[...]". If you want this, you'll have to either overload for pointers or do something like template<typename T> void func_impl(basic_path<T> file); void func(path file) { func_impl(file); } void func(wpath file) { func_impl(file); } In Christ, Steven Watanabe

On Tue, 09 Mar 2010 09:44:46 -0800, Steven Watanabe wrote:
Alex Lamaison wrote:
template<typename T> void func(basic_path<T> file) { do thing to file }
func("myfile"); func(L"myfile");
There's no way to get the compiler to deduce T, since the argument types are "const char[...]" and "const wchar_t[...]". If you want this, you'll have to either overload for pointers or do something like
template<typename T> void func_impl(basic_path<T> file);
void func(path file) { func_impl(file); } void func(wpath file) { func_impl(file); }
Thanks Steven. This is what I'll do. Looking forward to filesystem v3 which should fix this! Alex
participants (3)
-
Alex Lamaison
-
Alexander Lamaison
-
Steven Watanabe