简单结构

时间:2015-05-31 11:42:58

标签: c++ stream variadic-templates variadic

我的目标是有几个流出来。您可以在下面看到工作代码。

我正在尝试使用可变参数模板,但我失败了。下一个代码如何“变化”?

有几个非常相似的变量,所以我猜它可以使用可变参数模板重新编写,但我不知道如何。

template<typename T>
struct IsOn
{
 T *pt;
 bool isOn;
 IsOn(T& t, bool b):pt(&t),isOn(b) {}
};

struct tmy
{
 vector<IsOn<ostream>> v0;
 vector<IsOn<ofstream>> v1;
 vector<IsOn<stringstream>> v2;
};

template<typename T>
tmy& operator<<(tmy& rt,T& t) {
 int len;
 len=rt.v0.size();
 for(int i=0; i<len;++i) if(rt.v0[i].isOn) (*rt.v0[i].pt)<<t;
 len=rt.v1.size();
 for(int i=0; i<len;++i) if(rt.v1[i].isOn) (*rt.v1[i].pt)<<t;
 len=rt.v2.size();
 for(int i=0; i<len;++i) if(rt.v2[i].isOn) (*rt.v2[i].pt)<<t;
 return rt;
}


int main(int argc, char** argv) {
 tmy my;
 my.v0.push_back(IsOn<ostream>(cout, true));
 my.v0.push_back(IsOn<ostream>(cerr, false));
 my.v0.push_back(IsOn<ostream>(clog, true));
 my<<"hi twice!";
}

感谢您的尝试!

P.S。我知道有一个boost :: tee,但我的问题略有不同,可以在这里阅读:How to declare an "implicit conversion" in a variadic template?

2 个答案:

答案 0 :(得分:2)

好的,我得到了两件事,但我觉得它没有多大意义:

#include <iostream>

template<typename ... Streams>
struct StreamCont
{
};

template<typename Stream>
struct StreamCont<Stream>
{
    Stream & stream;
    StreamCont(Stream & st) :  stream(st) {};
};

template<typename Stream, typename ... Next>
struct StreamCont<Stream, Next...>
{
    Stream & stream;
    StreamCont<Next...> next;

    StreamCont(Stream & st, Next&... next) :  stream(st), next(next...) {};
};

template<typename Stream, typename Arg>
StreamCont<Stream>& operator<<(StreamCont<Stream> & str, Arg arg)
{
    str.stream << arg;
    return str;
};

template<typename ... Streams, typename Arg>
StreamCont<Streams...>& operator<<(StreamCont<Streams...> & str, Arg arg)
{
    str.stream << arg;
    str.next << arg;
    return str;
};

/* std::endl signature:
template< class CharT, class Traits >
std::basic_ostream<CharT, Traits>& endl( std::basic_ostream<CharT, Traits>& os );

so this only works if all streams are equal
*/

template<typename Stream>
StreamCont<Stream>& operator<<(StreamCont<Stream> & str, Stream&(*func)(Stream&) )
{
    func(str.stream);
    return str;
};


template<typename First, typename ... Streams>
StreamCont<First, Streams...>& operator<<(StreamCont<First, Streams...> & str, First&(*func)(First&) )
{
    func(str.stream);
    str.next << func;
    return str;
};


int main() 
{
    StreamCont<std::ostream, std::ostream, std::ostream> 
        multi_stream(std::cout, std::cerr, std::clog);

    multi_stream << 42 << std::endl;
    return 0;
}

用数组做同样的事情可能更有意义,即:

#include <iostream>
#include <array>


template<typename Stream, size_t Size, typename Arg>
std::array<Stream*, Size>& operator<<(std::array<Stream*, Size>& str, const Arg &arg)
{
    for (auto  s : str)
        *s << arg;
    return str;
};

template<typename Stream, size_t Size>
std::array<Stream*, Size>& operator<<(std::array<Stream*, Size>& str, Stream& (*func)(Stream&))
{
    for (auto  s : str)
        *s << func;
    return str;
};



int main() 
{
    std::array<std::ostream*, 3> strs =  {&std::cout, &std::cerr, &std::clog};

    strs << 42 << std::endl;
    return 0;
}

希望有所帮助,我不知道你是否可以更好地编写ostream的自定义重载(请参阅boost.iostreams)并使用stream_bufs并将它们组合到自定义类中。

答案 1 :(得分:2)

您的代码的可变版本可能如下所示:

template<typename T>
struct IsOn
{
    T *pt;
    bool isOn;
    IsOn(T& t, bool b):pt(&t), isOn(b) {}
};

template <typename ... Ts>
struct tmy
{
    std::tuple<std::vector<IsOn<Ts>>...> vs;
};

namespace detail
{
    template <typename T1, typename T2>
    void print(std::vector<IsOn<T1>>& v, T2& t)
    {
        for (auto&& e : v) {
            if (e.isOn) {
                (*e.pt) << t;
            }
        }
    }

    template <std::size_t ... Is, typename Tuple, typename T>
    void print(std::index_sequence<Is...>, Tuple& tuple, T& t)
    {
#if 1 // Folding expression of C++17
        (print(std::get<Is>(tuple, t)), ...);
#else
        int dummy[] = {0, (print(std::get<Is>(tuple, t)), 0)...};
        static_cast<void>(dummy); // avoid warning for unused variable
#endif
    }

}

template<typename ...Ts, typename T>
tmy<Ts...>& operator<<(tmy<Ts...>& rt,T& t) {
    detail::print(std::index_sequence_for<Ts...>{}, rt.vs, t);
    return rt;
}

Live demo