Skip to content

Instantly share code, notes, and snippets.

@slashvar
Created October 4, 2018 12:59
Show Gist options
  • Save slashvar/34d09b1e9fe29936a4f544684ff03df4 to your computer and use it in GitHub Desktop.
Save slashvar/34d09b1e9fe29936a4f544684ff03df4 to your computer and use it in GitHub Desktop.
C++ constructors check ...
/*
* checking some assumptions
* compile with: clang++ -Weverything -Wno-c++98-compat -std=c++17 -o noncopyable noncopyable.cc
*/
#include <iostream>
#include <type_traits>
class Default {};
class Uncopiable
{
public:
Uncopiable() = default;
Uncopiable(Uncopiable&& _c) = default;
Uncopiable& operator=(Uncopiable&&) = default;
};
static_assert(std::is_copy_constructible<Uncopiable>::value == false, "Uncopiable must be non copy constructible");
static_assert(std::is_copy_assignable<Uncopiable>::value == false, "Uncopiable must be non copy assignable");
class Unmovable
{
public:
~Unmovable() = default;
};
class No
{
public:
No() = delete;
No(No&&) = delete;
No(const No&) = delete;
~No() { std::cout << "Yes !" << std::endl; }
};
static No yes() { return No{}; }
int main()
{
std::cout << std::boolalpha
<< "is_default_constructible<Default>: " << std::is_default_constructible<Default>::value << std::endl;
std::cout << std::boolalpha
<< "is_copy_constructible<Default>: " << std::is_copy_constructible<Default>::value << std::endl;
std::cout << std::boolalpha
<< "is_copy_assignable<Default>: " << std::is_copy_assignable<Default>::value << std::endl;
std::cout << std::boolalpha
<< "is_move_constructible<Default>: " << std::is_move_constructible<Default>::value << std::endl;
std::cout << std::boolalpha
<< "is_move_assignable<Default>: " << std::is_move_assignable<Default>::value << std::endl;
std::cout << std::endl;
std::cout << std::boolalpha
<< "is_default_constructible<Uncopiable>: " << std::is_default_constructible<Uncopiable>::value << std::endl;
std::cout << std::boolalpha
<< "is_copy_constructible<Uncopiable>: " << std::is_copy_constructible<Uncopiable>::value << std::endl;
std::cout << std::boolalpha
<< "is_copy_assignable<Uncopiable>: " << std::is_copy_assignable<Uncopiable>::value << std::endl;
std::cout << std::boolalpha
<< "is_move_constructible<Uncopiable>: " << std::is_move_constructible<Uncopiable>::value << std::endl;
std::cout << std::boolalpha
<< "is_move_assignable<Uncopiable>: " << std::is_move_assignable<Uncopiable>::value << std::endl;
std::cout << std::endl;
std::cout << std::boolalpha
<< "is_default_constructible<Unmovable>: " << std::is_default_constructible<Unmovable>::value << std::endl;
std::cout << std::boolalpha
<< "is_copy_constructible<Unmovable>: " << std::is_copy_constructible<Unmovable>::value << std::endl;
std::cout << std::boolalpha
<< "is_copy_assignable<Unmovable>: " << std::is_copy_assignable<Unmovable>::value << std::endl;
std::cout << std::boolalpha
<< "is_move_constructible<Unmovable>: " << std::is_move_constructible<Unmovable>::value << std::endl;
std::cout << std::boolalpha
<< "is_move_assignable<Unmovable>: " << std::is_move_assignable<Unmovable>::value << std::endl;
std::cout << std::endl;
std::cout << std::boolalpha
<< "is_default_constructible<No>: " << std::is_default_constructible<No>::value << std::endl;
std::cout << std::boolalpha
<< "is_copy_constructible<No>: " << std::is_copy_constructible<No>::value << std::endl;
std::cout << std::boolalpha
<< "is_copy_assignable<No>: " << std::is_copy_assignable<No>::value << std::endl;
std::cout << std::boolalpha
<< "is_move_constructible<No>: " << std::is_move_constructible<No>::value << std::endl;
std::cout << std::boolalpha
<< "is_move_assignable<No>: " << std::is_move_assignable<No>::value << std::endl;
No n = yes();
(void)n;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment