From boost to std::experimental and furthermore c++17

烂漫一生 提交于 2019-12-11 04:59:59

问题


I commonly use boost to implement some features, specially ths boost::filesystem (1.58.0).

Also I use std::experimental to string_view (my compiler didn't include it as standard yet - g++ 5.4.0 20160609).

Because the boost features I use are aprooved I want to be ready to c++17.

Fortunaly I use the following commands in my code:

using namespace boost::filesystem; //the only exeption is to boost::filesystem::remove

using namespace std::experimental;

If I replace the boost line to 'using namespace std::experimental::filesystem;' I will get exactly the same behavior as boost implementation with change nothing more in my code?

And after I get the official gcc compiler with these features already include as standard all I need to do is: a) change the 'std::experimental::filesystem;' to 'std::filesystem' b) delete the line 'using namespace std::experimental;'

and get the same behavior with change nothing more in my code?

Which other boost features are included on c++17 and also can be easily replaced as describe above?


回答1:


Boost is not (affiliated with the) ISO standard.

No, you shouldn't blindly expect the semantics to be identical. Even though in 80%-90% of the cases the interface will be compatible, you will get differences.

E.g. boost::optional<T&> is allowed, but not in the standard version. There are other differences.

In general, it's bad practice to use using-directives, especially if you use it to paper of these differences. If you want to keep your dependencies mobile, it's probably best to create your own namespaces, wrapper functions.

In my code-base, e.g. I have used

namespace XXX {

     using nullopt_t = ::boost::none_t;
     static CONSTEXPR nullopt_t nullopt = {};

     template <typename T> using optional = ::boost::optional<T>;
}

We use this interface exclusively in any non-boost specific code.

This should aid in transitioning to the standard library version, except for semantic differences already outlined.




回答2:


Boost and the later derived standarizations tend to differ.

In the case of filesystem, there are a few breaking changes. Off the top of my head, a flag was changed to a bitfield of flags.

However 90% of the code works the same.

Instead of using namespace boost::filesystem;, consider using

#ifdef USE_BOOST_FILESYSTEM
  namespace filesystem = boost::filesystem;
#else
  namespace filesystem = std::experitmental::filesystem;
#endif

Now use filesystem:: to prefix your use of the API.

At the handful of spots where the implementation differs, you can use more #ifdef.

The things that are in that namespace now stick out in your code, and you have an #ifdef to handle the corner cases where they aren't compatible.



来源:https://stackoverflow.com/questions/44565019/from-boost-to-stdexperimental-and-furthermore-c17

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!