Skip to content

Commit

Permalink
range_formatter : サンプルコードのコンパイルが通らなさそうなので見直し
Browse files Browse the repository at this point in the history
  • Loading branch information
faithandbrave committed Jan 25, 2023
1 parent 63e429d commit 373f49d
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions reference/format/range_formatter.md
Expand Up @@ -41,7 +41,31 @@ namespace std {
#include <vector>
template <class T>
class std::range_formatter<std::vector<T>> : public std::range_formatter<std::vector<T>> {
class MyVector {
std::vector<T> v_;
public:
using base_type = std::vector<T>;
using iterator = typename base_type::iterator;
using const_iterator = typename base_type::iterator;
using value_type = typename base_type::value_type;
using reference = typename base_type::reference;
using const_reference = typename base_type::const_reference;
MyVector() = default;
MyVector(std::initializer_list<T> init)
: v_(init.begin(), init.end()) {}
iterator begin() { v_.begin(); }
const_iterator begin() const { v_.begin(); }
iterator end() { v_.end(); }
const_iterator end() const { v_.end(); }
const std::vector<T>& base() const { return v_; }
};
template <class T>
class std::range_formatter<MyVector<T>> : public std::range_formatter<std::vector<T>> {
bool is_colon = false;
using base_type = std::range_formatter<std::vector<T>>;
public:
Expand All @@ -63,7 +87,7 @@ public:
// format()関数は書式の情報をもたない。
// parse()関数で解析した書式をメンバ変数で保持しておいて、
// それをもとに書式化する
auto format(const std::vector<T>& v, std::format_context& fctx) const {
auto format(const MyVector<T>& v, std::format_context& fctx) const {
if (is_colon) {
auto out = fctx.out();
bool is_first = true;
Expand All @@ -80,10 +104,12 @@ public:
}
return out;
}
return base_type::format(v, fctx);
return base_type::format(v.base(), fctx);
}
};
#include <cstdint>
int main()
{
std::vector<std::uint8_t> v = {0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
Expand Down

0 comments on commit 373f49d

Please sign in to comment.