简体   繁体   English

使用Visual Studio 2012代码分析的错误C2248

[英]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: 尝试在此函数上运行Visual Studio 2012代码分析器时,我在函数的第一行遇到错误C2248:

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 ] ParseInput.cpp(24):错误C2248:'std :: basic_ifstream <_Elem,_Traits> :: basic_ifstream':无法使用[_Elem = char,_Traits访问类'std :: basic_ifstream <_Elem,_Traits>中声明的私有成员= std :: char_traits],同时检查是否可以使用[_Elem = char,_Traits =调用可忽略的复制构造函数'std :: basic_ifstream <_Elem,_Traits> :: basic_ifstream(const std :: basic_ifstream <_Elem,_Traits>&)' std :: char_traits] C:\\ Program Files(x86)\\ Microsoft Visual Studio 11.0 \\ VC \\ include \\ fstream(827):请参见带有[_Elem = char的'std :: basic_ifstream <_Elem,_Traits> :: basic_ifstream'的声明,使用[_Elem = char,_Traits = std :: char_traits]从'std :: basic_ifstream <_Elem,_Traits>'转换为'std :: basic_ifstream <_Elem,_Traits> &&'时,_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. 如所写,它应该构造一个临时的std :: ifstream,然后使用它来复制构造inputFile。

Apparently the normal compiler optimizes out the copy-construction so that the line ends up just constructing inputFile directly. 显然,普通的编译器优化了复制构造,因此该行最终只是直接构造inputFile。 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: 由于没有理由首先构造一个临时的ifstream,因此您可以将其重写为:

std::ifstream inputFile(m_filename);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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