简单结构体的可变模板示例



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

我试图使用可变模板,但我失败了。下一个代码如何"变化"?

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

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!";
}

谢谢你的努力!

注。我知道存在boost::tee,但我有一个稍微不同的问题,可以在这里阅读:如何声明一个"隐式转换";在可变模板中?

好了,我明白了,但是我不认为这有多大意义:

#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。使用stream_bufs并将它们组合到您的自定义类中。

您的代码的可变版本可能看起来像:

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;
}

现场演示

相关内容

  • 没有找到相关文章

最新更新