idaholab / moose

Multiphysics Object Oriented Simulation Environment
https://www.mooseframework.org
GNU Lesser General Public License v2.1
1.6k stars 1k forks source link

Enable implicit conversion between `FileName` and `std::filesystem::path` #27483

Open hugary1995 opened 3 weeks ago

hugary1995 commented 3 weeks ago

Motivation

FileName subclasses std::string, and so it will be two layers of implicit conversion from FileName to std::filesystem::path. Functions accepting std::filesystem::path will not take FileName, which seems counter-intuitive to me.

Design

Add the implicit conversion. But we need to be careful with the modification to the macro though, as we don't want other string-derived types like VariableName to be implicitly convertible to path.

Impact

Happier coders. We can do

foo(filename)

instead of

foo(std::string(filename))
hugary1995 commented 2 weeks ago

Current min clang 10.0.1 and min (redhat) gcc 8.5.0 don't like a conversion function to path. The error messages are like

note: while substituting deduced template arguments into function template 'path' [with _Source = FileName, _Require = (no value)]
    print_filename(f);
                   ^
/opt/compiler-explorer/gcc-9.3.0/lib/gcc/x86_64-linux-gnu/9.3.0/../../../../include/c++/9.3.0/bits/fs_path.h:93:7: note: candidate function [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
      __is_path_src(const basic_string<_CharT, _Traits, _Alloc>&, int);
      ^
/opt/compiler-explorer/gcc-9.3.0/lib/gcc/x86_64-linux-gnu/9.3.0/../../../../include/c++/9.3.0/bits/fs_path.h:101:7: note: candidate function [with _Unknown = FileName]
      __is_path_src(const _Unknown&, ...);

clang 12.0.1 and above seems to have fixed this bug.