initializer_lists of streams (C++11)

Viewed 204

I'm trying to pass variable number of ofstreams over to a function that accepts an initializer_list but doesn't seem to work and throws all possible errors from the initializer_list structure and about how my function is with an array of ofstreams cannot be matched to any defined function.

Is it actually possible to pass a reference of ofstreams over in an initializer_list?

test.cpp

#include "extension.h"


ofstream outputFile, outputFile2;

int main(void) {

    outputFile.open(("data_1.txt");
    outputFile2.open("data_2.txt");
    writeSomething({outputFile, outputFile2});
    outputFile.close();
    outputFile2.close();
}

extension.h

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <iostream>
#include <fstream>
#include <unistd.h>
#include <string.h>
#include <initializer_list>

using namespace std;

void writeSomething(initializer_list<ofstream&> args);

extension.cpp

#include "extension.h"

void writeSomething(initializer_list<ofstream&> args) {
    for (auto f : args ) {
        f << "hello" << endl;
    }
}
1 Answers
Related