Assert that code does NOT compile

后端 未结 6 957
长发绾君心
长发绾君心 2020-12-03 04:54

In short:

How to write a test, that checks that my class is not copyable or copy-assignable, but is only moveable and move-assignable?

6条回答
  •  半阙折子戏
    2020-12-03 05:50

    A good answer is given at the end of a great article "Diagnosable validity" by Andrzej Krzemieński:

    A practical way to check if a given construct fails to compile is to do it from outside C++: prepare a small test program with erroneous construct, compile it, and test if compiler reports compilation failure. This is how “negative” unit tests work with Boost.Build. For an example, see this negative test form Boost.Optional library: optional_test_fail_convert_from_null.cpp. In configuration file it is annotated as compile-fail, meaning that test passes only if compilation fails.

提交回复
热议问题