I'm having trouble trying to use an ifstream
from within a block. (This is part of a larger, complex project, so I whipped up a quick little source file with just the relevant parts.)
// foo.cpp, in its entirety:
#include <iostream>
#include <fstream>
#include <Block.h>
int main() {
__block std::ifstream file("/tmp/bar") ;
// ^ tried this with and without the __block
void (^block)() = ^{
file.rdbuf() ;
file.close() ;
file.open("/tmp/bar") ;
} ;
block() ;
}
If I declare the ifstream
with __block
, I get:
foo.cpp:6:24: error: call to implicitly-deleted copy constructor of
'std::ifstream' (aka 'basic_ifstream<char>')
__block std::ifstream file("/tmp/bar") ;
^~~~
If I declare it without __block
, I get:
foo.cpp:8:3: error: call to implicitly-deleted copy constructor of
'const std::ifstream' (aka 'const basic_ifstream<char>')
file.rdbuf() ;
^~~~
// rdbuf() and (presumably) other const functions
foo.cpp:9:3: error: member function 'close' not viable: 'this' argument has
type 'const std::ifstream' (aka 'const basic_ifstream<char>'), but
function is not marked const
file.close() ;
^~~~
// open(), close(), and (presumably) other non-const functions
What's the proper way to use fstream
s inside of blocks?