对宽字符串使用boost :: iostreams :: mapped_file_source [英] Using boost::iostreams::mapped_file_source with wide character strings

查看:642
本文介绍了对宽字符串使用boost :: iostreams :: mapped_file_source的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

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

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

I get the following compiler error in 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,我得到以下错误:

If I instead try to pass the constructor a boost::filesystem::path I get the following error:

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感觉我缺少一些明显的东西,但我只是在圈子中试图找出编译器试图告诉我,但我只是迷路了。那只手掌到额头的时刻只是没有发生..它是什么,我做错了?

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?

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

The constructor defined in mapped_file.hpp looks like the following:

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

basic_mapped_file_params类构造函数如下所示:

The basic_mapped_file_params class constructors look like this:

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

是在标题中的一些额外帮助:

There is some additional help in the header that says:

// 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 错误。

我知道编译器告诉我问题是什么,但是查看头源和评论,让我相信我想要完成的是支持,它只是我的方法是不正确的。我错误解释什么头文件告诉我吗?我知道这里有一个很好的模板实例化和显式/隐式转换的教训。

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. Am I misinterpreting what the header file is telling me? I know there is probably a good lesson about template instantiation and explicit/implicit conversion in here somewhere.

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

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.48我可以这样做。

With boost 1.48 I can do something like this.

#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(用于创建新文件)

or you can do this with mapped_file_params(used create new file)

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

这篇关于对宽字符串使用boost :: iostreams :: mapped_file_source的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆