2

如果我实例化一个mapped_file_source(升压1.46.1)用窄字符串如下面的我没有一个问题:使用boost ::输入输出流:: mapped_file_source宽字符串

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 ::文件系统::路径我得到以下错误:

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错误..

我知道的编译器告诉我是什么问题,但看着头源和评论导致我相信,我试图完成的是支持的,这只是我的方法是不正确的。我误解了头文件告诉我什么?我知道在这里某处可能有关于模板实例化和显式/隐式转换的很好的教训。

有趣的是,升级我的提振安装到1.47.0似乎清理C2664错误,但我仍然获取有关访问私有成员C2248错误。

回答

2

With 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; 
1

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

+0

我加入的问题一些额外的信息。我理解编译器告诉我什么,我只是不明白为什么,从mapped_file.hpp头文件的源头 –

+0

@ C.Trauma:如果这是头文件的样子,那么文档就过时了。我发现的文档页是从1.47.0。 –

+0

滚动到文档页面底部显示“2008年2月2日修订”,因此很可能它们已过期。 –

1

它看起来像是将mapped_file的文档是很老的,并不能反映什么是在标题或标题注释中。为了实例化一个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作为参数,这是代码无法编译的地方。