You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I was integrating the {fmt} library into my project and encounter the following issue: the compilation fails due to ambiguous partial template specialization when I include fmt/ranges.h and a template class have the methods begin(), end() and a custom formatter defined. For example,
#include <vector>
#include <fmt/format.h>
#include <fmt/ranges.h>
template<typename T>
struct Matrix {
using iterator = typename std::vector<T>::iterator;
int nrows;
int ncols;
std::vector<T> internal;
Matrix(int rows, int cols, std::initializer_list<T> ilist) : nrows(rows), ncols(cols), internal(ilist)
{}
T operator()(int i, int j) const {
return internal[j + i * ncols];
}
iterator begin() {
return internal.begin();
}
iterator end() {
return internal.end();
}
};
template<typename T>
struct fmt::formatter<Matrix<T>>
{
formatter<T> entry_fmt;
template<typename ParseContext>
constexpr auto parse(ParseContext& ctx) {
return entry_fmt.parse(ctx);
}
template<typename FormatContext>
auto format(const Matrix<T>& mat, FormatContext& ctx) {
auto output = ctx.out();
for (int i = 0; i < mat.nrows; ++i) {
for (int j = 0; j < mat.ncols; ++j) {
output = entry_fmt.format(mat(i, j), ctx);
format_to(output, " ");
}
format_to(output, "\n");
}
return output;
}
};
int main(int argc, char* argv[]) {
Matrix<float> mat1(3, 3, {1, 2, 3, 4, 5, 6, 7, 8, 9});
fmt::print("{}", mat1);
return EXIT_SUCCESS;
}
Naturally, removing the fmt/ranges.h header, everything works as expected. However, the issue arises when you have multiple classes: one using a custom formatter like this and another that uses a range formatter.
The text was updated successfully, but these errors were encountered:
Hello,
I was integrating the {fmt} library into my project and encounter the following issue: the compilation fails due to ambiguous partial template specialization when I include
fmt/ranges.h
and a template class have the methodsbegin()
,end()
and a custom formatter defined. For example,Live demo: https://godbolt.org/z/nY4771soe
Naturally, removing the
fmt/ranges.h
header, everything works as expected. However, the issue arises when you have multiple classes: one using a custom formatter like this and another that uses a range formatter.The text was updated successfully, but these errors were encountered: