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

to_string_view interferes with FMT_COMPILE formatting #2181

Closed
BillyDonahue opened this issue Mar 15, 2021 · 1 comment
Closed

to_string_view interferes with FMT_COMPILE formatting #2181

BillyDonahue opened this issue Mar 15, 2021 · 1 comment

Comments

@BillyDonahue
Copy link
Contributor

Similar to #2180 in outcome but I think underlying cause might be different.
https://gcc.godbolt.org/z/rWKEM6

#include <fmt/args.h>
#include <fmt/compile.h>
#include <fmt/format.h>

#include <iostream>
#include <string>
#include <string_view>

struct UserType {
  std::string name;

#ifdef HAS_TO_STRING_VIEW
  friend std::string_view to_string_view(const UserType& ut) { return ut.name; }
#endif
};

template <>
struct fmt::formatter<UserType> : fmt::formatter<std::string_view> {
  using B = fmt::formatter<std::string_view>;
  using B::parse;
  template <typename FC>
  auto format(const UserType& ut, FC& fc) {
    return B::format(std::string_view{ut.name}, fc);
  }
};

void print(const std::string& s) { std::cout << s << std::endl; }

int main() {
  UserType ut{"Alice"};
  print(fmt::format(FMT_COMPILE("[{}]"), ut));
  return 0;
}

Fails only if HAS_TO_STRING_VIEW is defined.
(see godbolt link for full failure output).

In file included from <source>:2:
/opt/compiler-explorer/libs/fmt/trunk/include/fmt/compile.h: In instantiation of 'constexpr OutputIt fmt::v7::detail::field<Char, T, N>::format(OutputIt, const Args& ...) const [with OutputIt = fmt::v7::detail::buffer_appender<char>; Args = {UserType}; Char = char; T = UserType; int N = 0]':
/opt/compiler-explorer/libs/fmt/trunk/include/fmt/compile.h:612:21:   required from 'constexpr OutputIt fmt::v7::detail::concat<L, R>::format(OutputIt, const Args& ...) const [with OutputIt = fmt::v7::detail::buffer_appender<char>; Args = {UserType}; L = fmt::v7::detail::field<char, UserType, 0>; R = fmt::v7::detail::code_unit<char>]'
/opt/compiler-explorer/libs/fmt/trunk/include/fmt/compile.h:613:22:   required from 'constexpr OutputIt fmt::v7::detail::concat<L, R>::format(OutputIt, const Args& ...) const [with OutputIt = fmt::v7::detail::buffer_appender<char>; Args = {UserType}; L = fmt::v7::detail::code_unit<char>; R = fmt::v7::detail::concat<fmt::v7::detail::field<char, UserType, 0>, fmt::v7::detail::code_unit<char> >]'
/opt/compiler-explorer/libs/fmt/trunk/include/fmt/compile.h:827:3:   required from 'std::__cxx11::basic_string<Char> fmt::v7::format(const CompiledFormat&, const Args& ...) [with CompiledFormat = fmt::v7::detail::concat<fmt::v7::detail::code_unit<char>, fmt::v7::detail::concat<fmt::v7::detail::field<char, UserType, 0>, fmt::v7::detail::code_unit<char> > >; Args = {UserType}; Char = char; typename std::enable_if<fmt::v7::detail::is_compiled_format<T>::value, int>::type <anonymous> = 0]'
/opt/compiler-explorer/libs/fmt/trunk/include/fmt/compile.h:877:18:   required from 'std::__cxx11::basic_string<typename Context::char_type> fmt::v7::format(const S&, Args&& ...) [with S = main()::<lambda()>::FMT_COMPILE_STRING; Args = {UserType&}; typename std::enable_if<fmt::v7::detail::is_compiled_string<S>::value, int>::type <anonymous> = 0; typename Context::char_type = char; typename Context::char_type = char]'
<source>:31:44:   required from here
/opt/compiler-explorer/libs/fmt/trunk/include/fmt/compile.h:546:25: error: no matching function for call to 'write<char>(fmt::v7::detail::buffer_appender<char>&, const UserType&)'
  546 |       return write<Char>(out, arg);
      |              ~~~~~~~~~~~^~~~~~~~~~
In file included from /opt/compiler-explorer/libs/fmt/trunk/include/fmt/compile.h:14,
                 from <source>:2:
/opt/compiler-explorer/libs/fmt/trunk/include/fmt/format.h:1789:24: note: candidate: 'template<class OutputIt, class StrChar, class Char> constexpr OutputIt fmt::v7::detail::write(OutputIt, fmt::v7::basic_string_view<Char>, const fmt::v7::basic_format_specs<Char>&)'
 1789 | FMT_CONSTEXPR OutputIt write(OutputIt out, basic_string_view<StrChar> s,
      |                        ^~~~~
@vitaut
Copy link
Contributor

vitaut commented Mar 19, 2021

Fixed in 14a2a64. Thanks for reporting.

@vitaut vitaut closed this as completed Mar 19, 2021
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

2 participants