ATul Singh
ATul Singh

Reputation: 532

VS2015 cannot convert from 'initializer list' to 'std::string' error

// 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 

Problem comes at the line : drive_path = std::string(path.begin(), r.begin());

This was compiling succesfully on VS2013 but its throwing an error in VS 2015 Error: error C2440: '': cannot convert from 'initializer list' to 'std::string'

As per std::string constructor we have a Range Constructor which takes iterator1 and iterator 2 which will fill the string.

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

    //range (7) 
template <class InputIterator>
  string  (InputIterator first, InputIterator last);

In VS2013 its now showing any warning of some potential problem and compiled succesfully.

Both the iterators are of the same string , i dont know why this is failing in VS2015 . Any help is appreciated.

Upvotes: 2

Views: 1791

Answers (2)

Werner Henze
Werner Henze

Reputation: 16771

string ctor requires both iterators being of the same type class InputIterator. Your path.begin() is a std::string::iterator whereas r.begin() is a std::string::const_iterator.

I'd give a try to either (making both const)

drive_path = std::string(path.cbegin(), r.begin());

or (making both non-const)

boost::iterator_range<std::string::iterator> r;

Upvotes: 5

roalz
roalz

Reputation: 2788

Iterator parameters should be of the same type for the string constructor you are using.
I suspect you are passing a "non-const" iterator (path.begin()) as first parameter, and a const_iterator as second.

You can try to pass both const_iterators (use string::cbegin()):

drive_path = std::string(path.cbegin(), r.begin());

Upvotes: 1

Related Questions