optional-lite
optional-lite copied to clipboard
Invalid copy/move constructible/assignable detection
Code:
#include <https://raw.githubusercontent.com/martinmoene/optional-lite/master/include/nonstd/optional.hpp>
#include <iostream>
#include <string>
struct A
{
A() = default;
A(const A &) = delete;
A& operator=(const A &) = delete;
A(A &&) = delete;
A& operator=(A &&) = delete;
};
int main()
{
std::cout << "Copy constructible: "
<< std::is_copy_constructible<A>::value << " "
<< std::is_copy_constructible<nonstd::optional<A>>::value
<< std::endl;
std::cout << "Move constructible: "
<< std::is_move_constructible<A>::value << " "
<< std::is_move_constructible<nonstd::optional<A>>::value
<< std::endl;
std::cout << "Copy assignable: "
<< std::is_copy_assignable<A>::value << " "
<< std::is_copy_assignable<nonstd::optional<A>>::value
<< std::endl;
std::cout << "Move assignable: "
<< std::is_move_assignable<A>::value << " "
<< std::is_move_assignable<nonstd::optional<A>>::value
<< std::endl;
}
Output:
Copy constructible: 0 1
Move constructible: 0 1
Copy assignable: 0 1
Move assignable: 0 1
Expected output:
Copy constructible: 0 0
Move constructible: 0 0
Copy assignable: 0 0
Move assignable: 0 0
Live demo: https://godbolt.org/z/a7fhv3
Added another demo at godbolt.
Relevant talks:
- How to Write Well-Behaved Value Wrappers - Simon Brand [C++ on Sea 2019]
- C++ Function Templates: How Do They Really Work? - Walter E. Brown [C++ on Sea 2019]
Output:
B / std::optional<B> - C++ std
Copy constructible: 00
Move constructible: 00
Copy assignable : 00
Move assignable : 00
Trivially destructable: 11
B / boost::optional<B> - Boost
Copy constructible: 01
Move constructible: 01
Copy assignable : 01
Move assignable : 01
Trivially destructable: 10
B / std::experimental::optional<B> - Andrzej Krzemieński
Copy constructible: 01
Move constructible: 01
Copy assignable : 01
Move assignable : 01
Trivially destructable: 11
B / tl::optional<B> - Sy Brand
Copy constructible: 00
Move constructible: 00
Copy assignable : 00
Move assignable : 00
Trivially destructable: 11
B / nonstd::optional<B> - this library
Copy constructible: 01
Move constructible: 01
Copy assignable : 01
Move assignable : 01
Trivially destructable: 10