【问题标题】:Issue getting `boost::filesystem::path` character pointer获取 `boost::filesystem::path` 字符指针的问题
【发布时间】:2019-11-18 01:51:24
【问题描述】:

在以下两行中,第一行给了我编译时错误,第二行很好:

    std::remove( boost::filesystem::path( mypath / "file.log" ).c_str() );
    std::remove( boost::filesystem::path( mypath / "file.log" ).string().c_str() );

std::remove 签名是:int remove( const char* fname );

这是错误信息:

“没有重载函数“std::remove”的实例与参数列表匹配”

boost::filesystem::path::c_str()std::string::c_str() 都返回const char*

我使用的编译器是 Visual C++ 2013。

【问题讨论】:

    标签: c++ visual-c++ boost path boost-filesystem


    【解决方案1】:

    但是 boost::filesystem::path::c_str() 和 std::string::c_str() 返回一个 const char*

    不,这不是真的。

    我们可以打开boost\filesystem\path.hpp源代码,看看那里发生了什么:

      template< typename Char, Char Separator, Char PreferredSeparator, Char Dot >
      struct path_constants
      {
        typedef path_constants< Char, Separator, PreferredSeparator, Dot > path_constants_base;
        typedef Char                                    value_type; // <---
        //...
      };
    
      class path :
        public filesystem::path_detail::path_constants<
    #ifdef BOOST_WINDOWS_API
          wchar_t, L'/', L'\\', L'.'  // [1]
    #else
          char, '/', '/', '.'
    #endif
        >
      {
    

    并且在 [1] 行中 wchar_t 作为第一个参数 Char 传递给 path_constants 模板,因此在 Windows 下 c_str 返回指向宽字符(2 个字节)的指针。

    【讨论】:

      猜你喜欢
      • 2016-06-02
      • 2019-01-31
      • 1970-01-01
      • 2012-09-20
      • 2019-09-29
      • 2012-07-06
      • 1970-01-01
      • 2011-01-24
      • 2011-04-23
      相关资源
      最近更新 更多