简体   繁体   English

使用带有宽字符串的 boost::iostreams::mapped_file_source

[英]Using boost::iostreams::mapped_file_source with wide character strings

If I instantiate a mapped_file_source (boost 1.46.1 ) with a narrow character string as in the following I don't have a problem:如果我用一个窄字符串实例化 mapped_file_source (boost 1.46.1 ),如下所示,我没有问题:

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

However if I try to use a wide string:但是,如果我尝试使用宽字符串:

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

I get the following compiler error in VC2010 SP1:我在 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'

If I instead try to pass the constructor a boost::filesystem::path I get the following error:如果我尝试向构造函数传递 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'

I feel like I'm missing something obvious, but I'm just running around in circles trying to figure out what the compiler is trying to tell me, but I'm just getting lost.我觉得我遗漏了一些明显的东西,但我只是在兜圈子,试图弄清楚编译器试图告诉我什么,但我只是迷路了。 That palm to forehead moment is just not happening.. What is it that I am doing incorrectly?那个手掌到额头的时刻只是没有发生..我做错了什么?

The constructor defined in mapped_file.hpp looks like the following: mapped_file.hpp 中定义的构造函数如下所示:

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

The basic_mapped_file_params class constructors look like this: basic_mapped_file_params class 构造函数如下所示:

// 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) { }

Where the template class is defined as:其中模板 class 定义为:

// 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 
{

There is some additional help in the header that says: header 中有一些额外的帮助说:

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

If I take this approach with:如果我采用这种方法:

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

I get the same C2664 error mentioned above..我得到了上面提到的相同的C2664错误..

I know the compiler is telling me what the problem is, but looking at the header source and the comments leads me to believe that what I'm trying to accomplish is supported, it's just my approach that is incorrect.我知道编译器告诉我问题出在哪里,但是查看 header 源代码和评论让我相信我试图完成的事情是受支持的,只是我的方法不正确。 Am I misinterpreting what the header file is telling me?我是否误解了 header 文件告诉我的内容? I know there is probably a good lesson about template instantiation and explicit/implicit conversion in here somewhere.我知道这里的某个地方可能有关于模板实例化和显式/隐式转换的好课。

Interestingly enough, upgrading my boost install to 1.47.0 seems to cleared up C2664 error but I'm still getting the C2248 error about access to the private member.有趣的是,将我的 boost 安装升级到 1.47.0 似乎清除了C2664错误,但我仍然收到有关访问私有成员的C2248错误。

With boost 1.48 I can do something like this.使用 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;
}

or you can do this with mapped_file_params(used create new file)或者您可以使用 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;

It's telling you that boost::iostreams::mapped_file_source 's constructor does not take a wchar_t* , nor does it take a boost::filesystem::path .它告诉您boost::iostreams::mapped_file_source的构造函数不需要wchar_t* ,也不需要boost::filesystem::path It only takes std::string , or types convertible to std::string .它只需要std::string或可转换为std::string的类型。 Or, to put it another way, you can't use UTF-16 paths with this object.或者,换句话说,你不能在这个 object 中使用 UTF-16 路径。

It looks like the documentation for mapped_file is pretty old and does not reflect what is in the header or in the header comments.看起来 mapped_file 的文档很旧,并且没有反映 header 或 header 评论中的内容。 In order to instantiate a boost::iostreams:mapped_file_source object with a wide character string you need to explicity pass in the boost::iostreams::detail::path like this:为了使用宽字符串实例化boost::iostreams:mapped_file_source object,您需要显式传入boost::iostreams::detail::path ,如下所示:

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

I was able to get this to compile by stepping thought the error messages and determining how the template classes were being instantiated and finally saw that boost::iostreams::detail::path had a private constructor that took a &std::wstring as a parameter which is where the code was failing to compile.我能够通过逐步考虑错误消息并确定模板类的实例化方式来编译它,最后看到 boost::iostreams::detail::path 有一个私有构造函数,它采用 &std::wstring 作为参数,这是代码无法编译的地方。

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

相关问题 使用 boost::iostreams::mapped_file_source 和 std::multimap - Using boost::iostreams::mapped_file_source with std::multimap 隐式声明的 boost::iostreams::mapped_file_source 已弃用 - Implicitly-declared boost::iostreams::mapped_file_source is deprecated 未定义的引用`boost :: iostreams :: mapped_file_source :: mapped_file_source()' - undefined reference to `boost::iostreams::mapped_file_source::mapped_file_source()' 使用CMake对boost :: iostreams :: mapped_file_source :: init()的未定义引用 - undefined reference to boost::iostreams::mapped_file_source::init() using CMake boost::iostreams::mapped_file_source 打开一个具有 CJK 文件名的文件 - boost::iostreams::mapped_file_source opens a file that has CJK filename 如何使用带有gzip压缩输入文件的boost :: iostreams :: mapped_file_source - how to use boost::iostreams::mapped_file_source with a gzipped input file boost :: iostreams :: mapped_file_source :: open在Windows上导致退出代码3,但可在Ubuntu中使用 - boost::iostreams::mapped_file_source::open causes exit code 3 on Windows but works in Ubuntu boost mapping_file_source会抛出哪些异常? - What exceptions does boost mapped_file_source throw? 提高mapd_file_source,对齐方式和页面大小 - Boost mapped_file_source, alignment and page size 由openfilename引起的boost mapping_file_source异常 - boost mapped_file_source exception caused by openfilename
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM