Yarp: Make getDirectorySeparator aligned to std::filesystem::path::preferred_separator

Created on 12 Sep 2018  路  8Comments  路  Source: robotology/yarp

C++17 will introduce the filesystem header that has the preferred_separator constexpr defined as follow:

#ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
    typedef wchar_t             value_type;
    static constexpr value_type         preferred_separator = L'\\';
#else
    typedef char                value_type;
    static constexpr value_type         preferred_separator = '/';
#endif

It is premature to introduce it in yarp since we are supporting only c++11( and filesystem is marked experimental) but my suggestion is to define this constexpr in YARP_OS, something like:

namespace yarp {
namespace os {
namespace filesystem {
#ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
    typedef wchar_t             value_type;
    static constexpr value_type         preferred_separator = L'\\';
#else
    typedef char                value_type;
    static constexpr value_type         preferred_separator = '/';
#endif
} // namespace filesystem
} // namespace os
} // namespace yarp

In order to simply make it a typedef of std::filesystem::path::preferred_separator once we will support c++17.

Moreover I'd suggest to change yarp::os::NetworkBase::getDirectorySeparator() in:

std::string NetworkBase::getDirectorySeparator() {
    return yarp::os::filesystem::preferred_separator;
}

Deprecating it with a deprecation warning like ("Use yarp::os::filesystem::preferred_separator instead").

I know that deprecating could be annoying for users, but I think that this function is used inside yarp but not so much used in user code.

Finally note that a constexpr is defined a compile time, then more optimized respect to a function call.

Related to #1755 and #1853 .

Library - YARP_os YARP v3.3.0 Fixed Modernization

Most helpful comment

By the way, I do not know the meaning of L'\\' ? Is that something C++17 or libstdcxx specific?

That's how you define a "wide char" wchar_t (standard c++98).

All 8 comments

:+1: , just pay attention that in YARP code the macro used should not be _GLIBCXX_FILESYSTEM_IS_WINDOWS , as this will not work if libcxx or the MSVC standard library is used.

By the way, I do not know the meaning of L'\\' ? Is that something C++17 or libstdcxx specific?

By the way, I do not know the meaning of L'\' ? Is that something C++17 or libstdcxx specific?

No idea, I found only this: https://stackoverflow.com/questions/6384118/what-does-the-l-in-front-a-string-mean-in-c

By the way, I do not know the meaning of L'\\' ? Is that something C++17 or libstdcxx specific?

That's how you define a "wide char" wchar_t (standard c++98).

It is a "wide char" because '\\' has the dimension of 2 chars, am I right?

No, \\ is one character represented in one byte (char), L'\\' is still a single character, but represented with 2 bytes (wchar_t)

Fixed by #2062

Was this page helpful?
0 / 5 - 0 ratings

Related issues

traversaro picture traversaro  路  4Comments

diegoferigo picture diegoferigo  路  3Comments

abolfazlzaraki picture abolfazlzaraki  路  3Comments

Nicogene picture Nicogene  路  4Comments

drdanz picture drdanz  路  3Comments