How to write `is_complete` template?

The answer given by Alexey Malistov can be used on MSVC with a minor modification: namespace { template<class T, int discriminator> struct is_complete { static T & getT(); static char (& pass(T))[2]; static char pass(…); static const bool value = sizeof(pass(getT()))==2; }; } #define IS_COMPLETE(X) is_complete<X,__COUNTER__>::value Unfortunately, the __COUNTER__ predefined macro is not part of … Read more

Detect existence of private member

There is indeed a way for non-final non-union class types: namespace detail { struct P {typedef int member;}; template <typename U> struct test_for_member : U, P { template <typename T=test_for_member, typename = typename T::member> static std::false_type test(int); static std::true_type test(float); }; } template <typename T> using test_for_member = std::integral_constant<bool, decltype(detail::test_for_member<T>::test(0)){}>; Demo. The trick is to … Read more

When is a lambda trivial?

The standard does not specify whether a closure type (the type of a lambda expression) is trivial or not. It explicitly leaves this up to implementation, which makes it non-portable. I am afraid you cannot rely on your static_assert producing anything consistent. Quoting C++14 (N4140) 5.1.2/3: … An implementation may define the closure type differently … Read more

How to write a trait which checks whether a type is iterable

You may create a trait for that: namespace detail { // To allow ADL with custom begin/end using std::begin; using std::end; template <typename T> auto is_iterable_impl(int) -> decltype ( begin(std::declval<T&>()) != end(std::declval<T&>()), // begin/end and operator != void(), // Handle evil operator , ++std::declval<decltype(begin(std::declval<T&>()))&>(), // operator ++ void(*begin(std::declval<T&>())), // operator* std::true_type{}); template <typename T> std::false_type … Read more

Why is char distinct from signed char and unsigned char?

There are three distinct basic character types: char, signed char and unsigned char. Although there are three character types, there are only two representations: signed and unsigned. The (plain)char uses one of these representations. Which of the other two character representations is equivalent to char depends on the compiler. In an unsigned type, all the … Read more

Why can you implement std::is_function in terms of std::is_const and std::is_reference?

Let’s go over the conditions as they appear: If const T isn’t const (const doesn’t really apply to function types since functions aren’t objects), and T isn’t a reference (const doesn’t apply to references either for the same reason), it’s a function type. int (or any other non-function-non-reference type) wouldn’t fit in because is_const<const int>::value … Read more