简体   繁体   中英

Error C2248 using Visual Studio 2012 code analysis

While attempting to run the Visual Studio 2012 code analyzer on this function, I encounter error C2248 on the first line of the function:

void InputFile::parseInputFile()
{
    auto inputFile = std::ifstream(m_filename); // error occurs on this line
    if (inputFile.is_open())
    {
        const auto fileBegin = std::istreambuf_iterator<char>(inputFile);
        const auto fileEnd   = std::istreambuf_iterator<char>();

        const auto fileContents = std::string(fileBegin, fileEnd);
        m_sectors.reserve(std::count(std::begin(fileContents), std::end(fileContents), '\n'));

        const auto lineTokenizer = boost::tokenizer<boost::char_separator<char>>(fileContents, boost::char_separator<char>("\n"));
        const auto symbolSeparator = boost::char_separator<char>(" ");

        std::transform(std::begin(lineTokenizer), std::end(lineTokenizer), std::back_inserter(m_sectors), 
            [=](const std::string& line) 
        {
            const auto symbolTokenizer = boost::tokenizer<boost::char_separator<char>>(line, symbolSeparator);

            std::vector<std::string> symbols;
            symbols.reserve(std::count(std::begin(line), std::end(line), ' '));
            std::copy(std::begin(symbolTokenizer), std::end(symbolTokenizer), std::back_inserter(symbols));
            return symbols;
        });
    }
}

The full error message is below:

ParseInput.cpp(24): error C2248: 'std::basic_ifstream<_Elem,_Traits>::basic_ifstream' : cannot access private member declared in class 'std::basic_ifstream<_Elem,_Traits>' with [ _Elem=char, _Traits=std::char_traits ] while checking that elided copy-constructor 'std::basic_ifstream<_Elem,_Traits>::basic_ifstream(const std::basic_ifstream<_Elem,_Traits> &)' is callable with [ _Elem=char, _Traits=std::char_traits ] C:\\Program Files (x86)\\Microsoft Visual Studio 11.0\\VC\\include\\fstream(827) : see declaration of 'std::basic_ifstream<_Elem,_Traits>::basic_ifstream' with [ _Elem=char, _Traits=std::char_traits ] when converting from 'std::basic_ifstream<_Elem,_Traits>' to 'std::basic_ifstream<_Elem,_Traits> &&' with [ _Elem=char, _Traits=std::char_traits ]

The error message doesn't get triggered if I'm not running code analysis. What specific check is triggering this error, and is it possible to disable it so analysis can complete on my code?

I don't believe streams are supposed to be copyable. However, the line in question is potentially doing exactly that. As written it should construct a temporary std::ifstream and then copy-construct inputFile using that.

Apparently the normal compiler optimizes out the copy-construction so that the line ends up just constructing inputFile directly. And apparently this is bypassing any checking of whether copy-construction would have been allowed at all.

Evidently code analysis does a more thorough check and is catching the discrepancy.

Since there is no reason to be constructing a temporary ifstream in the first place, you can just rewrite the line as:

std::ifstream inputFile(m_filename);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM