VS2015 无法从“初始化列表”转换为“std::string”错误

ATu*_*ngh 2 c++ string stdstring visual-studio-2013 visual-studio-2015

// get the drive letter/network volume portion of the path
    std::string drive_path;
    if (drive_type == disk_util::drive_type_network)
    {
        boost::iterator_range<std::string::const_iterator> r = boost::find_nth(path, "/", 3);
        if (!r.empty())
            drive_path = std::string(path.begin(), r.begin());
    }
    else
    {
        size_t i = path.find('/');
        drive_path = path.substr(0, i);
    }


//path is also a std::string type 
Run Code Online (Sandbox Code Playgroud)

问题出在这一行:drive_path = std::string(path.begin(), r.begin());

这是在 VS2013 上成功编译,但它在 VS 2015 中抛出错误 Error: error C2440: '': cannot convert from 'initializer list' to 'std::string'

根据 std::string 构造函数,我们有一个 Range Constructor,它接受迭代器 1 和迭代器 2 来填充字符串。

http://www.cplusplus.com/reference/string/string/string/

    //range (7) 
template <class InputIterator>
  string  (InputIterator first, InputIterator last);
Run Code Online (Sandbox Code Playgroud)

在 VS2013 中,它现在显示了一些潜在问题的警告并成功编译。

两个迭代器都是同一个字符串,我不知道为什么这在 VS2015 中失败。任何帮助表示赞赏。

Wer*_*nze 5

stringctor 要求两个迭代器的类型相同class InputIterator。你path.begin()是一个std::string::iterator而是r.begin()一个std::string::const_iterator

我会尝试(使两者都为常量)

drive_path = std::string(path.cbegin(), r.begin());
Run Code Online (Sandbox Code Playgroud)

或(使两个非常量)

boost::iterator_range<std::string::iterator> r;
Run Code Online (Sandbox Code Playgroud)