How to tell if a type is an instance of a specific template class?

Here’s an option: #include <iostream> #include <type_traits> #include <string> template <class, template <class> class> struct is_instance : public std::false_type {}; template <class T, template <class> class U> struct is_instance<U<T>, U> : public std::true_type {}; template <class> class Second {}; int main() { using A = Second<int>; using B = Second<std::string>; using C = float; std::cout … Read more

should std::common_type use std::decay?

should std::common_type use std::decay? Yes, see Library Working Group Defect #2141. Short version (long version, see link above): declval<A>() returns a A&& common_type is specified via declval, n3337: template <class T, class U> struct common_type<T, U> { typedef decltype(true ? declval<T>() : declval<U>()) type; }; common_type<int, int>::type therefore yields int&&, which is unexpected proposed resolution … Read more

“What happened to my SFINAE” redux: conditional template class members?

Firstly, C++11 did not carry forward boost’s disable_if. So if you’re going to transition boost code, you’ll need to use enable_if with a negated condition (or redefine your own disable_if construct). Secondly, for SFINAE to reach in and apply to the method level, those methods must be templates themselves. Yet your tests have to be … Read more

How to test whether class B is derived from template family of classes

Try this: #include <type_traits> template <typename T, template <typename> class Tmpl> // #1 see note struct is_derived { typedef char yes[1]; typedef char no[2]; static no & test(…); template <typename U> static yes & test(Tmpl<U> const &); static bool const value = sizeof(test(std::declval<T>())) == sizeof(yes); }; Usage: #include <iostream> template<class T> struct X {}; struct … Read more

Check if a type is passed in variadic template parameter pack

You’re looking for std::disjunction. It’s specified in N4564 [meta.logical]. #include <type_traits> template<typename T, typename… Ts> constexpr bool contains() { return std::disjunction_v<std::is_same<T, Ts>…>; } static_assert( contains<int, bool, char, int, long>()); static_assert( contains<bool, bool, char, int, long>()); static_assert( contains<long, bool, char, int, long>()); static_assert(not contains<unsigned, bool, char, int, long>()); Live demo Or, adapted to a struct template<typename … Read more

What is the purpose of C++20 std::common_reference?

common_reference came out of my efforts to come up with a conceptualization of STL’s iterators that accommodates proxy iterators. In the STL, iterators have two associated types of particular interest: reference and value_type. The former is the return type of the iterator’s operator*, and the value_type is the (non-const, non-reference) type of the elements of … Read more

Why is std::is_pod deprecated in C++20?

POD is being replaced with two categories that give more nuances. The c++ standard meeting in november 2017 had this to say about it: Deprecating the notion of “plain old data” (POD). It has been replaced with two more nuanced categories of types, “trivial” and “standard-layout”. “POD” is equivalent to “trivial and standard layout”, but … Read more

How to tell if template type is an instance of a template class?

Here’s an option: #include <iostream> #include <type_traits> #include <string> template <class, template <class> class> struct is_instance : public std::false_type {}; template <class T, template <class> class U> struct is_instance<U<T>, U> : public std::true_type {}; template <class> class Second {}; int main() { using A = Second<int>; using B = Second<std::string>; using C = float; std::cout … Read more