系统相关
首页 > 系统相关> > c – 这是boost :: filesystem中的错误吗?为什么boost :: filesystem :: path :: string()在Windows和Linux上没有相同的签名?

c – 这是boost :: filesystem中的错误吗?为什么boost :: filesystem :: path :: string()在Windows和Linux上没有相同的签名?

作者:互联网

我正在尝试使用成员函数string()将boost :: filesystem :: path的向量转换为std :: string.我写了这个,它在Windows上工作正常(MSVC 14,2015):

std::transform(
    users.begin(), users.end(), std::back_inserter(usersStrs),
    std::mem_fn(static_cast<const std::string (PathType::*)() const>(
        &PathType::string)));

现在我转到gcc(6.3,Debian Stretch),我的代码给出了上面签名不存在的链接错误.要修复它,我不得不将代码更改为:

std::transform(
    users.begin(), users.end(), std::back_inserter(usersStrs),
    std::mem_fn(static_cast<const std::string& (PathType::*)() const>(
        &PathType::string)))

PS:我知道lambda解决方案更容易,我现在切换到了必需的解决方案.

起初,我认为MSVC更宽容,但后来我切换回Windows并得到相反的链接错误,第一个签名是正确的.我去了源代码(1.64,path.hpp),这是我发现的:

#   ifdef BOOST_WINDOWS_API
    const std::string string() const
    {
      std::string tmp;
      if (!m_pathname.empty())
        path_traits::convert(&*m_pathname.begin(), &*m_pathname.begin()+m_pathname.size(),
        tmp);
      return tmp;
    }
//...
#   else   // BOOST_POSIX_API
    //  string_type is std::string, so there is no conversion
    const std::string&  string() const { return m_pathname; }
//...
#   endif

所以我看到的原因是在Windows上,因为它默认不使用UTF-8,所以有一个临时转换.但为什么不提升Windows和Linux使用相同的API?最坏的情况是,它会花费一个字符串的副本.对?

我应该使用path :: string()的替代方法来实现跨平台的API稳定性吗?

解决方法:

您可能正在使用旧版本的Boost.Filesystem库. Boost 1.64 says的签名是:

string string(const codecvt_type& cvt=codecvt()) const;

返回类型不依赖于平台;它应该始终是一个值,而不是一个参考.请注意,这(大部分)匹配the C++17 FileSystem library’s definition.因此,如果您在文档说它是一个值时获得引用,那么其中一个是错误的.因此,无论哪种方式都存在错误.

但是,应该注意的是,在C标准中(因此,也可能在Boost中),成员函数的假设是它们不必与文档规范完全匹配.例如,成员函数可以具有标准中未列出的其他默认参数.只要它如所述那样可调用,那就是一个有效的实现.

因此,你不应该期望std :: mem_fn完全像这样工作.使用C标准的措辞,不应该假设path :: string可以转换为具有该签名的成员指针.因此,虽然它可能不一致,但是您可以获得成员指针的期望可能不是Boost支持的接口.

无论是否是错误,您都可以使用lambda轻松解决这个问题:

std::transform(
    users.begin(), users.end(), std::back_inserter(usersStrs),
    [](const auto &pth) -> decltype(auto) {return pth.string();});

它比std :: mem_fn版本更清晰.如果返回引用,则decltype(auto)会阻止不必要的副本.

标签:c-2,linux,c11,boost,boost-filesystem
来源: https://codeday.me/bug/20190527/1163488.html