简体   繁体   English

使用std :: wcout的C ++中的高阶函数失败,错误C2248

[英]Higher-order functions in C++, using std::wcout fails with error C2248

I'm playing around with implementing functional-style stuff in C++. 我正在玩用C ++实现功能样式的东西。 At the moment, I'm looking at a continuation-passing style for enumerating files. 目前,我正在寻找一种枚举文件的连续传递样式。

I've got some code that looks like this: 我有一些看起来像这样的代码:

namespace directory
{
    void find_files(
        std::wstring &path,
        std::function<void (std::wstring)> process)
    {
        boost::filesystem::directory_iterator begin(path);
        boost::filesystem::directory_iterator end;

        std::for_each(begin, end, process);
    }
}

Then I'm calling it like this: 然后我这样称呼它:

directory::find_files(source_root, display_file_details(std::wcout));

...where display_file_details is defined like this: ...其中display_file_details的定义如下:

std::function<void (std::wstring)>
    display_file_details(std::wostream out)
{
    return [&out] (std::wstring path) { out << path << std::endl; };
}

The plan is to pass a continuation to find_files , but to be able to pass composed functions into it. 计划是将继续传递给find_files ,但是要能够将组合函数传递给它。

But I get the error: 但是我得到了错误:

error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' :
    cannot access private member declared in
    class 'std::basic_ios<_Elem,_Traits>'

What am I doing wrong? 我究竟做错了什么? Am I insane for trying this? 我为这个而疯狂吗?

Note: my functional terminology (higher-order, continuations, etc.) is probably wrong. 注意:我的功能术语(高阶,连续等)可能是错误的。 Feel free to correct me. 随时纠正我。

In display_file_details , you need to take your wostream by reference. display_file_details ,您需要参考一下wostream。 iostream copy constructors are private. iostream复制构造函数是私有的。

Upon looking more deeply into the compiler output, I found this: 在更深入地了解编译器输出后,我发现了这一点:

This diagnostic occurred in the compiler generated function
    'std::basic_ostream<_Elem,_Traits>::
        basic_ostream(const std::basic_ostream<_Elem,_Traits> &)'

It turns out that basic_ostream doesn't have an available copy constructor. 事实证明basic_ostream没有可用的副本构造函数。

Changing std::wostream out to std::wostream & out fixes it. std::wostream out更改为std::wostream & out修复该问题。 At least to the point that I get a bunch of other errors. 至少到了其他错误的地步。 These were easily fixed by: 这些很容易通过以下方式解决:

std::for_each(begin, end,
    [&process] (boost::filesystem::directory_entry d)
    { process(d.path().wstring()); });

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

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