Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strange interaction with {fmt} and std::filesystem/std::experimental::filesystem #729

Closed
LarsGullik opened this issue May 7, 2018 · 7 comments

Comments

@LarsGullik
Copy link

I have a small test that gives weird results:

(test transcribed so might contain errors)

#include <cassert>

int main()
{
    auto d = std::string{"somedir/somepath"};
    auto p = std::experimental::filesystem::path(d);

   auto s = fmt::format("{}", p);

   assert(d == p.native());
   assert(s == p.native()); // Fails.
}

s ends up having some weird contents that I cannot see how it gets.
(this is with GCC-8 in C++14 mode.)

@LarsGullik
Copy link
Author

Typically the fist path component ("somedir") ends up as null bytes in s.

@vitaut
Copy link
Contributor

vitaut commented May 10, 2018

The problem is implicit conversion from path to a temporary string that goes out of scope before formatting. I need to think how to catch such cases.

@vitaut
Copy link
Contributor

vitaut commented May 10, 2018

The regression was introduced in 18ac987. Before that it would result in a compile-time error.

@vitaut
Copy link
Contributor

vitaut commented May 19, 2018

Disabled the unsafe conversion in d940fa6. Now the problematic code will result in a compile-time error which can be resolved either by falling back to ostream (include ostream.h) or by providing a formatter for path:

namespace fmt {
template <>
struct formatter<std::experimental::filesystem::path> : formatter<std::string> {};
}

Thanks for catching this!

@vitaut vitaut closed this as completed May 19, 2018
@alabuzhev
Copy link
Contributor

Rather than disabling this, wouldn't it be better to replace
return vformat(format_str, make_args(args...));
with something like
return vformat_variadic_wrapper(format_str, make_arg(args)...));
i.e. perform any conversions first to prolong the lifetime of such temporaries and only then capture them to arg_store?

@vitaut
Copy link
Contributor

vitaut commented May 20, 2018

@alabuzhev, I was thinking of ways to extend lifetimes of temporaries, but all the solutions that I've found so far, including the additional indirection that you suggest, require extra hoops from the user (and still possible to misuse). Since the use of implicit conversions is normally discouraged anyway I think it's better to prioritize the simplicity of argument capturing API. Types with implicit conversions are easy to support through the normal formatter API.

@alabuzhev
Copy link
Contributor

@vitaut, thanks for the explanation - I didn't realise that there's an API which might get too complicated so it just looked like a purely artificial limitation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants