trivial vs. standard layout vs. POD

I don’t think it can be done in truly layman’s terms, at least without a lot of extra explanation. One important point is static vs. dynamic initialization, but explaining that to a layman would be several pages in itself… PODs were (mis-)defined in C++98. There are really two separate intents involved, neither expressed very well: … Read more

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

Check traits for all variadic template arguments

Define all_true as template <bool…> struct bool_pack; template <bool… v> using all_true = std::is_same<bool_pack<true, v…>, bool_pack<v…, true>>; And rewrite your constructor to // Check convertibility to B&; also, use the fact that getA() is non-const template<typename… Args, typename = std::enable_if_t<all_true<std::is_convertible<Args&, B&>{}…>> C(Args&… args) : member{args.getA()…} {} Alternatively, under C++17, template<typename… Args, typename = std::enable_if_t<(std::is_convertible_v<Args&, B&> … Read more

Which cannot be implemented without compiler hooks?

I have written up a complete answer here — it’s a work in progress, so I’m giving the authoritative hyperlink even though I’m cutting-and-pasting the text into this answer. Also see libc++’s documentation on Type traits intrinsic design. is_union is_union queries an attribute of the class that isn’t exposed through any other means; in C++, … Read more

Disambiguate overloaded member function pointer being passed as template parameter

The issue is here: l.call(&foo::func, “hello”); l.call(&foo::func, 0.5); For both lines, the compiler doesn’t know which foo::func you are referring to. Hence, you have to disambiguate yourself by providing the type information that is missing (i.e., the type of foo:func) through casts: l.call(static_cast<void (foo::*)(const std::string&)>(&foo::func), “hello”); l.call(static_cast<void (foo::*)(const double )>(&foo::func), 0.5); Alternatively, you can provide … Read more

Is it possible to determine if a type is a scoped enumeration type?

I think testing if it is an enum and not implicitly convertible to the underlying type should do the trick. template <typename T, bool B = std::is_enum<T>::value> struct is_scoped_enum : std::false_type {}; template <typename T> struct is_scoped_enum<T, true> : std::integral_constant<bool, !std::is_convertible<T, typename std::underlying_type<T>::type>::value> {};

What is decltype with two arguments?

It’s an comma-separated list of expressions, the type is identical to the type of the last expression in the list. It’s usually used to verify that the first expression is valid (compilable, think SFINAE), the second is used to specify that decltype should return in case the first expression is valid.

Check if a variable 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

What do compilers do with compile-time branching?

TL;DR There are several ways to get different run-time behavior dependent on a template parameter. Performance should not be your primary concern here, but flexibility and maintainability should. In all cases, the various thin wrappers and constant conditional expressions will all be optimized away on any decent compiler for release builds. Below a small summary … Read more