Boost logo

Boost Users :

From: Stewart Tootill (stewart.tootill_at_[hidden])
Date: 2005-11-24 05:00:20


I've just run into an interesting problem with boost::filesystem::path and thought I would raise it somewhere.

When I run the following code, compiled with MSVC7.1, on windows XP I get an exception thrown when calling some_path_function.

The exception is causes by boost::filesystem::path's implicit conversion constructor constructing a path from a string using the posix path matching rules. Obviously I can correct the problem by always using the native path checker argument, and that is fine, but the fact that this is an implicit conversion makes it hard to spot and I (and my colleagues) are not always that awake :).

So I suppose the question is a) can the default path checking function be changed in such a way that on windows platforms it validates the path using windows rules and on posix platforms using posix rules, or b) should the constructor at fault here be made explicit since it can really catch you out.

#include <boost/filesystem/path.hpp>
#include <boost/filesystem/exception.hpp>
#include <iostream>
#include <string>

void some_path_function( const boost::filesystem::path& path ) {
        std::cout << "Got a path : " << path.native_file_string() << std::endl;
}

int main() {
        try {
                std::string winPath = "c:\\windows\\system32\\kernel32.dll";
                some_path_function( winPath );
        } catch ( boost::filesystem::filesystem_error& err ) {
                std::cerr << "An error occured : " << err.what() << std::endl;
        }

        return 0;
}

Stewart Tootill
Senior Engineer [Digital Text]
Tel: +44 118 984 2151 - Fax: +44 118 984 3939
mailto:stewart.tootill_at_[hidden]
www.softel.co.uk

________________________________________________________________________
This e-mail has been scanned for viruses by MessageLabs.



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net