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 Alan Walker

Different World flac

Alan Walker. 2018. Writer: Shy Nodi;Alan Walker;Fredrik Borch Olsen;James Njie;Marcus Arnbekk;Gunnar Greve Pettersen;K-391;Corsak;Shy Martin;Magnus Bertelsen.
2

Tell Me It's Over

3 Ariana Grande

​Thank U, Next flac

Ariana Grande. 2018. Writer: Crazy Mike;Scootie;Victoria Monét;Tayla Parx;TBHits;Ariana Grande.
4 Mesto

Wait Another Day flac

Mesto. 2018.
5 Anne-Marie

Rewrite The Stars flac

Anne-Marie. 2018. Writer: Benj Pasek;Justin Paul.
6 Rita Ora

Let You Love Me flac

Rita Ora. 2018. Writer: Rita Ora;Easyfun;Fred Gibson;Noonie Bao;LotusIV;Ilsey Juber.
7 Conor Maynard

How You Love Me flac

Conor Maynard. 2018. Writer: Yoshi Breen;Thom Bridges;Hardwell;Rik Annema;Conor Maynard;Cimo Fränkel;Snoop Dogg.
8 Fitz And The Tantrums

HandClap flac

Fitz And The Tantrums. 2017. Writer: Fitz And The Tantrums;Eric Frederic;Sam Hollander.
9 ZAYN

There You Are flac

ZAYN. 2018. Writer: Joe Garrett;Levi Lennox;Michael Hannides;Anthony Hannides;ZAYN.
10 (G)I-DLE

POP/STARS flac

(G)I-DLE. 2018. Writer: Riot Music Team;Harloe.
11 Clean Bandit

Baby flac

Clean Bandit. 2018. Writer: Jack Patterson;Kamille;Jason Evigan;Matthew Knott;Marina;Luis Fonsi.
12 Bastille

Grip flac

Bastille. 2018. Writer: Dan Smith;Espen Berg;Joakim Haukaas;Mark Crew;Simen Eriksrud;Thomas Eriksen.
13 Ariana Grande

Imagine flac

Ariana Grande. 2018. Writer: Jameel Roberts;Priscilla Renea;Happy Perez;Andrew "Pop" Wansel;Ariana Grande.
14 Mark Ronson

Nothing Breaks Like A Heart flac

Mark Ronson. 2018. Writer: Thomas Brenneck;Maxime Picard;Ilsey Juber;Conor Szymanski;Clement Picard;Mark Ronson;Miley Cyrus.
15 ZAYN

Good Years flac

ZAYN. 2018. Writer: Anthony Hannides;Khaled Rohaim;Michael Hannides;ZAYN;Herbie Crichlow.
16 The Chainsmokers

Beach House flac

The Chainsmokers. 2018. Writer: Andrew Taggart.
17 Clean Bandit

Mama flac

Clean Bandit. 2018. Writer: Jason Evigan;Jack Patterson;Grace Chatto;Ellie Goulding;Caroline Ailin.
18 Halsey

Without Me (Illenium Remix) flac

Halsey. 2018. Writer: Halsey;Delacey;Louis Bell;Amy Allen;Justin Timberlake;Timbaland;Scott Storch.
19 Martin Garrix

Dreamer (Nicky Romero Remix) flac

Martin Garrix. 2018. Writer: Yury Parkhomenko;Aleksandr Parkhomenko;Ilsey Juber;Brian Lee;Mike Yung;Martin Garrix.
20 Julian Jordan

Glitch flac

Julian Jordan. 2018.

Related questions

Hot questions

Language

Popular Tags