Any way to wrap construction of boost “tee” stream for automatic type deduction?

Catskul 09/06/2018. 1 answers, 50 views
c++ c++11 boost boost-iostreams

boost::iostreams::tee and company have a bit of a noisy/repetitive usage as seen here:

C++ "hello world" Boost tee example program

The goal is to make something like:

auto myTeeStream = make_tee(std::cout, myfile);

After trying to wrap this usage with a function make_tee to allow for automatic type deduction on the arguments I realized that both copy and move constructors are unavailable for the necessary types.

So: is there a sane way to wrap tee stream creation in c++11?

Here's my attempt that fails to compile because of deleted copy constructors and missing move constructors:

#include <iostream>
#include <ostream>
#include <fstream>

#include <boost/iostreams/tee.hpp>
#include <boost/iostreams/stream.hpp>

template <typename StreamT1, typename StreamT2>
boost::iostreams::stream<boost::iostreams::tee_device<StreamT1, StreamT2> >
make_tee(StreamT1 & t1, StreamT2 & t2)
{
    using boost::iostreams::stream;
    using boost::iostreams::tee;
    return stream<decltype(tee(t1,t2))>(tee(t1,t2)); // compile error
    //return std::move(stream<decltype(tee(t1,t2))>(tee(t1,t2))); // also fails of course
}


int main()
{
    {
        // desired usage
        std::ofstream myFile("file.txt");
        auto && myTee = make_tee(std::cout, myFile); // required from here
    }
    {
        // noisy default usage
        std::ofstream myFile("file.txt");
        using boost::iostreams::tee;
        using boost::iostreams::stream;
        auto && myTee = stream<decltype(tee(std::cout, myFile))>(tee(std::cout, myFile));
    }
    return 0;
}

Error from clang++ --std=c++11 teetest.cpp is:

teetest.cpp:14:12: error: call to implicitly-deleted copy constructor of 'boost::iostreams::stream<boost::iostreams::tee_device<basic_ostream<char>, basic_ofstream<char> > >'

1 Answers


Jarod42 09/06/2018.

Compile fine in c++17 with "guaranty copy elision".

In c++11, you might use return {..}:

template <typename StreamT1, typename StreamT2>
boost::iostreams::stream<boost::iostreams::tee_device<StreamT1, StreamT2> >
make_tee(StreamT1 & t1, StreamT2 & t2)
{
    using boost::iostreams::stream;
    using boost::iostreams::tee;
    return {tee(t1,t2)};
}

Demo


HighResolutionMusic.com - Download Hi-Res Songs

1 AJR

Birthday Party flac

AJR. 2019. Writer: Adam Met;Jack Met;Ryan Met;Peter Ivers;David Lynch.
2 Loote

Your Side Of The Bed flac

Loote. 2018. Writer: ​Jesse Saint John;Jackson Foote;Emma Lov Block.
3 AJR

100 Bad Days flac

AJR. 2019. Writer: Jack Met;Adam Met;Ryan Met.
4 Joe Jonas

Longer Than I Thought flac

Joe Jonas. 2018. Writer: Patrick Nissley;Jackson Foote;Dave Katz.
5 Loote

Out Of My Head flac

Loote. 2018. Writer: Emma Lov Block;Michael Pollack;Jeremy Dussolliet;Jackson Foote.
6 Iselin Solheim

Anyone Out There flac

Iselin Solheim. 2019. Writer: Iselin Solheim;Max Grahn.
7 Loote

Wish I Never Met You flac

Loote. 2018. Writer: Jackson Foote;Alex Peter Koste;Jeremy Dussolliet;Emma Lov Block.
8 Kim Petras

Heart To Break flac

Kim Petras. 2018. Writer: Cirkut;Aaron Joseph;Dr. Luke;Jacob Kasher;Kim Petras.
9 A L E X

Out On The Trampoline At Night flac

A L E X. 2018. Writer: A L E X.
10 A L E X

I Want To Hold Your Hand flac

A L E X. 2018. Writer: A L E X.
11 A L E X

Field flac

A L E X. 2018. Writer: A L E X.
12 A L E X

Save Me flac

A L E X. 2018. Writer: A L E X.
13 Devin

Summer Lover flac

Devin. 2019. Writer: Tommy Lee James;Stuart Crichton;Oliver Heldens;Nile Rodgers;Devin Guisande.
14 A L E X

9 To 5 flac

A L E X. 2018. Writer: A L E X.
15 A L E X

Skirt flac

A L E X. 2018. Writer: A L E X.
16 Florian Picasso

Midnight Sun (Extended Version) flac

Florian Picasso. 2019.
17 Florian Picasso

Midnight Sun flac

Florian Picasso. 2019.
18 21 Savage

Enzo flac

21 Savage. 2019. Writer: YungLunchBox;Sheck Wes;Offset;Gucci Mane;21 Savage;DJ Snake.
19 Tales Of Ratatösk

Battle Of The Doomed Gods 320kbps

Tales Of Ratatösk. 2019.
20 Tales Of Ratatösk

Andro 320kbps

Tales Of Ratatösk. 2019.

Related questions

Hot questions

Language

Popular Tags