by Anonymous » 10 Apr 2025, 01:00
Ich habe einen älteren Code aktualisiert, um STD :: Format zu verwenden, und war überrascht zu entdecken, dass es trotz dieser Tatsache funktioniert hat, dass ich vergessen hatte, ein Std :: Formatter Spezialisierung für diesen Typ bereitzustellen. Wenn der benutzerdefinierte Typ öffentlich beginnt und enden Methoden, formatiert die Bibliothek die Sequenz als von der Kommas getrennte Liste in quadratischen Klammern. (Oder etwas anderes?) < /P>
Hier ist ein in sich geschlossener Repro-Repro:
Code: Select all
#include
#include
class MyType {
public:
MyType() : m_values{1, 2, 3, 4} {}
using internal_type = std::array;
using const_iterator = typename internal_type::const_iterator;
const_iterator cbegin() const { return m_values.cbegin(); }
const_iterator cend() const { return m_values.cend(); }
const_iterator begin() const { return cbegin(); }
const_iterator end() const { return cend(); }
private:
internal_type m_values;
};
int main() {
MyType foo;
// Since MyType is a user-defined type, I would not
// expect this print statement to compile without a
// specialization of std::formatter, but because
// it's iterable, it prints: "foo = [1, 2, 3, 4]\n".
std::print("foo = {}\n", foo);
return 0;
}
Ich verwende MS VC ++ aus Visual Studio 17.12.15 und kompilieren Sie mit /std:c++Latest.
Ich habe einen älteren Code aktualisiert, um STD :: Format zu verwenden, und war überrascht zu entdecken, dass es trotz dieser Tatsache funktioniert hat, dass ich vergessen hatte, ein Std :: Formatter Spezialisierung für diesen Typ bereitzustellen. Wenn der benutzerdefinierte Typ öffentlich beginnt und enden Methoden, formatiert die Bibliothek die Sequenz als von der Kommas getrennte Liste in quadratischen Klammern. (Oder etwas anderes?) < /P>
Hier ist ein in sich geschlossener Repro-Repro:[code]#include
#include
class MyType {
public:
MyType() : m_values{1, 2, 3, 4} {}
using internal_type = std::array;
using const_iterator = typename internal_type::const_iterator;
const_iterator cbegin() const { return m_values.cbegin(); }
const_iterator cend() const { return m_values.cend(); }
const_iterator begin() const { return cbegin(); }
const_iterator end() const { return cend(); }
private:
internal_type m_values;
};
int main() {
MyType foo;
// Since MyType is a user-defined type, I would not
// expect this print statement to compile without a
// specialization of std::formatter, but because
// it's iterable, it prints: "foo = [1, 2, 3, 4]\n".
std::print("foo = {}\n", foo);
return 0;
}
[/code]
Ich verwende MS VC ++ aus Visual Studio 17.12.15 und kompilieren Sie mit /std:c++Latest.