获取“boost::文件系统::p ath”字符指针时出现问题

Issue getting `boost::filesystem::path` character pointer

本文关键字:指针 字符 问题 ath boost 文件系统 获取      更新时间:2023-10-16

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

    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。

但是 boost::filesystem::p ath::c_str(( 和 std::string::c_str(( 返回常量字符*

不,这不是真的。

我们可以打开boostfilesystempath.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 个字节(的指针。