Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/132.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用boosthof实现STL打印_C++_C++11_Boost_Containers_String Formatting - Fatal编程技术网

C++ 使用boosthof实现STL打印

C++ 使用boosthof实现STL打印,c++,c++11,boost,containers,string-formatting,C++,C++11,Boost,Containers,String Formatting,任意类型的漂亮打印容器 以下几行代码提供与中相同的输出, 但输出流仅限于std::cout 如何使用boost::hof重写这些代码,以提供print(std::ostream&,…)类似的接口 #包括 #包括 BOOST\u HOF\u STATIC\u LAMBDA\u函数(简单打印)=//BOOST::HOF::proj( boost::hof::fix(boost::hof::的第一个( [](auto,const auto&x)->decltype(std::cout只需添加一个参数来

任意类型的漂亮打印容器

以下几行代码提供与中相同的输出, 但输出流仅限于
std::cout

如何使用
boost::hof
重写这些代码,以提供
print(std::ostream&,…)
类似的接口

#包括
#包括
BOOST\u HOF\u STATIC\u LAMBDA\u函数(简单打印)=//BOOST::HOF::proj(
boost::hof::fix(boost::hof::的第一个(

[](auto,const auto&x)->decltype(std::cout只需添加一个参数来传递ostream就足够了:

BOOST_HOF_STATIC_LAMBDA_FUNCTION(simple_print_ex) = boost::hof::fix(
    boost::hof::first_of(
        [](auto, auto& os, const auto &x) -> decltype(os << x, void()) { os << x; },
        [](auto self, auto& os, const auto &range) -> decltype(self(os, *std::begin(range)), void()) {
            bool sep = false;
            os << '{';
            for (const auto &x : range) {
                sep = !sep || os << ',';
                self(os, x);
            }
            os << '}';
        },
        [](auto self, auto& os, const auto &tuple) {
            using namespace boost::hof;
            os << '(';
            bool sep = false;
            unpack(proj([&](const auto &i) {
                sep = !sep || os << ',';
                self(os, i);
            }))(tuple);
            os << ')';
        }));

template <typename Ostream, typename... Args> void print_ex(Ostream& os, Args &&... args) { simple_print_ex(os, std::make_tuple(std::forward<Args>(args)...)); }
表册

std::ofstream ofs("test.txt");
print_ex(ofs, t3, "xxxx", 55, m, std::vector<std::string>{ "x" }, XX{ 66 });
ofs << "\n";
#include <boost/hof.hpp>
#include <iostream>

BOOST_HOF_STATIC_LAMBDA_FUNCTION(simple_print_ex) = boost::hof::fix(
    boost::hof::first_of(
        [](auto, auto& os, const auto &x) -> decltype(os << x, void()) { os << x; },
        [](auto self, auto& os, const auto &range) -> decltype(self(os, *std::begin(range)), void()) {
            bool sep = false;
            os << '{';
            for (const auto &x : range) {
                sep = !sep || os << ',';
                self(os, x);
            }
            os << '}';
        },
        [](auto self, auto& os, const auto &tuple) {
            using namespace boost::hof;
            os << '(';
            bool sep = false;
            unpack(proj([&](const auto &i) {
                sep = !sep || os << ',';
                self(os, i);
            }))(tuple);
            os << ')';
        }));

template <typename Ostream, typename... Args> void print_ex(Ostream& os, Args &&... args) { simple_print_ex(os, std::make_tuple(std::forward<Args>(args)...)); }
template <typename... Args> void print(Args &&... args) { print_ex(std::cout, std::forward<Args>(args)...); }

//---- user code ---
struct XX {
    int n = 0;

    friend std::ostream &operator<<(std::ostream &os, const XX &o) { return os << o.n << "XX"; }
};

#include <map>
#include <vector>
#include <fstream>
int main() {
    using namespace std::string_literals;

    std::vector v = { 1, 2, 3, 4 };
    std::map m { std::pair { "a"s, 30 }, { "bb", 31 }, { "ccc", 32 } };

    auto t = std::make_tuple(6, 7, 8, 9);
    auto t2 = std::make_tuple(11, std::ref(v), t);
    auto t3 = std::make_tuple(t2, std::vector{ 1234, 23, 2, 3, 3 }, "abc",
                              std::vector{ std::vector{ 11, 12, 13 }, std::vector{ 15, 16, 17 }, std::vector{ 19 } });
    std::ofstream ofs("test.txt");
    print_ex(ofs, t3, "xxxx", 55, m, std::vector<std::string>{ "x" }, XX{ 66 });
    ofs << "\n";

    print(t3, "xxxx", 55, m, std::vector<std::string>{ "x" }, XX{ 66 });
}
#包括
#包括
BOOST_HOF_STATIC_LAMBDA_函数(简单打印)=BOOST::HOF::fix(
boost::hof::第一个(

[](auto,auto&os,const auto&x)->decltype(os谢谢你的回答。通过你的代码,我找到了阻止我添加Ostream参数的原因,这是第一个
boost::hof::proj
旨在实现
模板无效打印(Args&…Args){simple_打印(std::forward(Args);}
#include <boost/hof.hpp>
#include <iostream>

BOOST_HOF_STATIC_LAMBDA_FUNCTION(simple_print_ex) = boost::hof::fix(
    boost::hof::first_of(
        [](auto, auto& os, const auto &x) -> decltype(os << x, void()) { os << x; },
        [](auto self, auto& os, const auto &range) -> decltype(self(os, *std::begin(range)), void()) {
            bool sep = false;
            os << '{';
            for (const auto &x : range) {
                sep = !sep || os << ',';
                self(os, x);
            }
            os << '}';
        },
        [](auto self, auto& os, const auto &tuple) {
            using namespace boost::hof;
            os << '(';
            bool sep = false;
            unpack(proj([&](const auto &i) {
                sep = !sep || os << ',';
                self(os, i);
            }))(tuple);
            os << ')';
        }));

template <typename Ostream, typename... Args> void print_ex(Ostream& os, Args &&... args) { simple_print_ex(os, std::make_tuple(std::forward<Args>(args)...)); }
template <typename... Args> void print(Args &&... args) { print_ex(std::cout, std::forward<Args>(args)...); }

//---- user code ---
struct XX {
    int n = 0;

    friend std::ostream &operator<<(std::ostream &os, const XX &o) { return os << o.n << "XX"; }
};

#include <map>
#include <vector>
#include <fstream>
int main() {
    using namespace std::string_literals;

    std::vector v = { 1, 2, 3, 4 };
    std::map m { std::pair { "a"s, 30 }, { "bb", 31 }, { "ccc", 32 } };

    auto t = std::make_tuple(6, 7, 8, 9);
    auto t2 = std::make_tuple(11, std::ref(v), t);
    auto t3 = std::make_tuple(t2, std::vector{ 1234, 23, 2, 3, 3 }, "abc",
                              std::vector{ std::vector{ 11, 12, 13 }, std::vector{ 15, 16, 17 }, std::vector{ 19 } });
    std::ofstream ofs("test.txt");
    print_ex(ofs, t3, "xxxx", 55, m, std::vector<std::string>{ "x" }, XX{ 66 });
    ofs << "\n";

    print(t3, "xxxx", 55, m, std::vector<std::string>{ "x" }, XX{ 66 });
}