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

发布于 2024-12-07 10:14:38 字数 1101 浏览 3 评论 0原文

我正在尝试用 C++ 实现函数式的东西。目前,我正在研究用于枚举文件的连续传递样式。

我有一些代码如下所示:

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);
    }
}

然后我这样调用它:

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

...其中 display_file_details 定义如下:

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

计划是将延续传递给 find_files< /code>,但能够将组合函数传递给它。

但我收到错误:

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

我做错了什么?我尝试这个是疯了吗?

注意:我的函数术语(高阶、延续等)可能是错误的。请随时纠正我。

I'm playing around with implementing functional-style stuff in 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:

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笑忘罢 2024-12-14 10:14:38

display_file_details 中,您需要通过引用获取您的 wostream。 iostream 复制构造函数是私有的。

In display_file_details, you need to take your wostream by reference. iostream copy constructors are private.

睫毛溺水了 2024-12-14 10:14:38

在更深入地查看编译器输出后,我发现了这一点:

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

事实证明 basic_ostream 没有可用的复制构造函数。

将 std::wostream out 更改为 std::wostream & out 修复了它。至少到了我收到一堆其他错误的程度。这些很容易通过以下方式修复:

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

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.

Changing std::wostream out to std::wostream & out fixes it. 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()); });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文