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作爲參數,這是代碼無法編譯的地方。