【问题标题】:Using boost::iostreams::mapped_file_source with wide character strings使用带有宽字符串的 boost::iostreams::mapped_file_source
【发布时间】:2011-07-13 04:03:13
【问题描述】:

如果我用一个窄字符串实例化 mapped_file_source (boost 1.46.1 ),如下所示,我没有问题:

boost::iostreams::mapped_file_source m_file_( "testfile.txt" );

但是,如果我尝试使用宽字符串:

boost::iostreams::mapped_file_source m_file_( L"testfile.txt" );

我在 VC2010 SP1 中收到以下编译器错误:

P:\libs\boost_1_46_1\boost/iostreams/device/mapped_file.hpp(128): error C2248: 'boost::iostreams::detail::path::path' : cannot access private member declared in class 'boost::iostreams::detail::path'
          P:\libs\boost_1_46_1\boost/iostreams/detail/path.hpp(111) : see declaration of 'boost::iostreams::detail::path::path'>
          P:\libs\boost_1_46_1\boost/iostreams/detail/path.hpp(37) : see declaration of 'boost::iostreams::detail::path'

如果我尝试将 boost::filesystem::path 传递给构造函数,则会收到以下错误:

P:\libs\boost_1_46_1\boost/iostreams/device/mapped_file.hpp(128): error C2664: 'boost::iostreams::detail::path::path(const std::string &)' : cannot convert parameter 1 from 'const boost::filesystem3::path' to 'const std::string &'
         Reason: cannot convert from 'const boost::filesystem3::path' to 'const std::string'

我觉得我遗漏了一些明显的东西,但我只是在绕圈子试图弄清楚编译器试图告诉我什么,但我只是迷路了。那个手掌到额头的时刻并没有发生..我做错了什么?

mapped_file.hpp 中定义的构造函数如下所示:

// Constructor taking a parameters object
template<typename Path>
explicit mapped_file_source(const basic_mapped_file_params<Path>& p);

basic_mapped_file_params 类构造函数如下所示:

// Construction from a Path
explicit basic_mapped_file_params(const Path& p) : path(p) { }

// Construction from a path of a different type
template<typename PathT>
explicit basic_mapped_file_params(const PathT& p) : path(p) { }

其中模板类定义为:

// This template allows Boost.Filesystem paths to be specified when creating or
// reopening a memory mapped file, without creating a dependence on
// Boost.Filesystem. Possible values of Path include std::string,
// boost::filesystem::path, boost::filesystem::wpath, 
// and boost::iostreams::detail::path (used to store either a std::string or a
// std::wstring).
template<typename Path>
struct basic_mapped_file_params 
    : detail::mapped_file_params_base 
{

标题中有一些额外的帮助:

// For wide paths, instantiate basic_mapped_file_params 
// with boost::filesystem::wpath

如果我采用这种方法:

boost::iostreams::basic_mapped_file_params<boost::filesystem::wpath> _tmp(L"test.txt");
boost::iostreams::mapped_file_source m_file_( _tmp );

我得到了上面提到的同样的 C2664 错误..

我知道编译器告诉我问题出在哪里,但是查看头文件源和 cmets 让我相信我想要完成的工作是受支持的,只是我的方法不正确。我是否误解了头文件告诉我的内容?我知道这里可能有一个关于模板实例化和显式/隐式转换的好课。

有趣的是,将我的 boost 安装升级到 1.47.0 似乎清除了 C2664 错误,但我仍然收到有关访问私有成员的 C2248 错误。

【问题讨论】:

    标签: c++ visual-studio-2010 boost boost-filesystem boost-iostreams


    【解决方案1】:

    使用 boost 1.48 我可以做这样的事情。

    #include <boost/filesystem.hpp>
    #include <boost/iostreams/device/mapped_file.hpp>
    #include <iostream>
    
    int main()
    { 
      boost::filesystem::path p(L"b.cpp");
      boost::iostreams::mapped_file file(p); // or mapped_file_source
      std::cout << file.data() << std::endl;
    }
    

    或者您可以使用 mapped_file_params 来执行此操作(用于创建新文件)

    boost::filesystem::path p(L"aa");
    basic_mapped_file_params<boost::filesystem::path> param; // template param
    param.path = p;
    param.new_file_size = 1024;
    

    【讨论】:

      【解决方案2】:

      它告诉你boost::iostreams::mapped_file_source 的构造函数不需要wchar_t*,也不需要boost::filesystem::path。它只需要std::string,或可转换为std::string 的类型。或者,换句话说,您不能对这个对象使用 UTF-16 路径。

      【讨论】:

      • 我添加了一些关于该问题的附加信息。我明白编译器告诉我什么,我只是不明白为什么,从 mapped_file.hpp 头文件的源头
      • @C.Trauma:如果标题是这样的,那么文档已经过时了。我发现的文档页面来自 1.47.0。
      • 滚动到文档页面底部显示“2008 年 2 月 2 日修订”,因此很有可能它们已经过时了..
      【解决方案3】:

      mapped_file 的文档看起来很旧,并且没有反映标头或标头 cmets 中的内容。为了实例化具有宽字符串的boost::iostreams:mapped_file_source 对象,您需要像这样显式传入boost::iostreams::detail::path

      boost::iostreams::mapped_file_source m_file_( boost::iostreams::detail::path(boost::filesystem::path(L"testfile.txt")) );
      

      我能够通过逐步思考错误消息并确定模板类的实例化方式来编译它,最后看到 boost::iostreams::detail::path 有一个采用 &std:: 的私有构造函数wstring 作为参数,这是代码编译失败的地方。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-05-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-14
        • 2010-11-18
        • 1970-01-01
        • 2020-07-19
        相关资源
        最近更新 更多