As a challenge/fun activity/task, I have implemented my version of std::is_constructible
.
Here is the source code:
#include <cbrainx/cbrainx.hpp>
#include <iostream>
template <typename T, typename ...V>
auto aux(long) -> cbx::false_type;
template <typename T, typename ...V>
static auto aux(int) -> cbx::type_switch_t<
cbx::branch<cbx::false_type,
cbx::detail::void_t<decltype(::new(cbx::declval<void *>()) T{cbx::declval<V>()...})>>,
cbx::otherwise<cbx::true_type>>;
template <typename T, typename ...V>
auto is_constructible() -> decltype(aux<T, V...>(0));
struct X {
X() {
std::cout << "constructed..." << std::endl;
}
private:
X(int) {
}
public:
X(const X &) = delete;
X(X &&) {}
~X() {
std::cout << "destroyed..." << std::endl;
}
static void xd() {
X{1};
std::cout << decltype(is_constructible<X, int>())::value << std::endl;
}
};
auto main() -> int {
std::cout << std::boolalpha;
std::cout << decltype(is_constructible<X, X>())::value << std::endl;
std::cout << decltype(is_constructible<X, X &>())::value << std::endl;
std::cout << decltype(is_constructible<X, X &&>())::value << std::endl;
return 0;
}
IMPORTANT:
cbx::false_type
is equivalent tostd::false_type
. It has avalue
of typebool
. Likewisecbx::true_type
.cbx::type_switch_t
is equivalent toswitch
clause but, for types and evaluated statically at compile time.cbx::branch
is like theif
branch. The first argument is the condition and must have avalue
of typebool
. The second argument is the type itself.cbx::otherwise
is like theelse
branch.
Things I know
- It does not work for private constructors. (I am okay with that!)
- It's a rough implementation.
Things I want to know
- Is this implementation correct?
- Am I missing anything?
- How close it is to the actual
std::is_constructible
? - How can I improve?
C++ Standard: 20