CppCon 2014: Walter E. Brown "Modern Template Metaprogramming: A Compendium, Part I"
Why Stack Overflow recommends it
Stack Overflow users link it as the go-to introduction to template metaprogramming and as the source of the void_t SFINAE detection idiom that many answers reuse.
Recommended by 11 people across 11 different months, 2014 to 2024.
What it is
Walter E. Brown's CppCon 2014 talk (part one of two) on modern C++ template metaprogramming. He builds up techniques such as SFINAE, enable_if and the new void_t idiom by reimplementing standard library facilities like integral_constant and rank.
What people said
Strong rec Benjamin Lindley · 2015-03 · Stack Overflow · on “why shouldn't these conditions work for template types?”
Please? Please what? If you want an intro template metaprogramming, I would suggest watching Walter Brown's excellent talks from cppcon 2014. [this video]
Strong rec LoPiTaL · 2015-09 · Stack Overflow · on “Understanding C++ template metaprogramming”
Also you could see the talk by Walter E. Brown at CppCon2014. A really good one about template metaprogramming: [this video] . This is the first part of the talk. You can find the second part in the suggestions
Recommends nonsensation · 2014-12 · Stack Overflow · on “How do we use void_t for SFINAE?”
I watched Walter Brown's talk at Cppcon14 about modern template programming ( Part I [this video] , Part II [LINK] ) where he presented his void_t SFINAE technique. Example: Given a simple variable template that evaluates to void if all template arguments are well formed: template< class ... > using void_t = void; and the following trait that checks for the existence of a data member called member : template< class , class = void > struct has_member : std::false_type { }; // specialized as has_member< T , void > or discarded (SFINAE) template< class T > struct has_member< T , void_t< decltype( T::member ) > > : std::true_type { }; I tried to understand why and how this works. Therefore a tiny exa
Recommends Paul · 2015-09 · Stack Overflow · on “Best introduction to C++ template metaprogramming?”
Thank you @jwfearn, I read the article, although I found this more interesting: [this video] (it has two parts). It is a talk by Walter E. Brown: Modern Template Metaprogramming: A Compendium
Recommends vsoftco · 2015-12 · Stack Overflow · on “How much do I need to know about compile time vs runtime to comfortably write code in general”
@TonyD Thanks! Although type traits and `void_t` SFINAE [this video] go a long way, almost towards a (compile-time) "reflective-able" C++.
Recommends p0pa · 2016-02 · Stack Overflow · on “How to restrict typenames in template”
<type_traits> let you generalize your logic into class template. How this works is we take template parameter T and parameter pack of Ts and we start checking if T is same to Head of the list of types. If match is found, we inherit from std::true_type and we are done. If no match is found, we pop the head and recursively instantiate same template with tail of Ts . Eventually, if no match is found at all, parameter pack size will drop to zero and the compiler will instantiate base template class that inherit from std::false_type . Please check this video [this video] for much better and in dept explanation by Mr. Walter E. Brown. template<class T, class...> struct is_any_of: std::false_type{}; tem
Recommends Qaz · 2016-07 · Stack Overflow · on “How to check if a class has an inherited function at compile-time?”
Here's a solution [LINK] using void_t , which is standard in C++17 and also comes with additional utilities such as is_detected_exact in the Library Fundamentals v2 TS, taking most of the work out of has_push_back : template<typename... Ts> using void_t = void; template<typename T> using push_back_test = decltype(std::declval<T>().push_back(std::declval<typename T::const_reference>())); template<typename T, typename = void> struct has_push_back : std::false_type {}; template<typename T> struct has_push_back<T, void_t<push_back_test<T>>> : std::is_same<push_back_test<T>, void> {}; Or with future utilities [LINK] : template<typename T> using push_back_test = decltype(std::declval<T>().push_bac
Recommends SU3 · 2017-01 · Stack Overflow · on “How do you assert that an object has a certain method in c++?”
Another approach (from Walter Brown: Part I [this video] , Part II [LINK] ) is to use void_t [LINK] . Here's an example of a trait that checks if you can call emplace_back member function: template <typename... T> struct make_void { typedef void type;}; template <typename... T> using void_t = typename make_void<T>::type; template <typename, typename = void> struct has_size : std::false_type { }; template <typename T> struct has_size<T, void_t<decltype( std::declval<T&>().size() )> > : std::true_type { }; int main() { std::cout << has_size<std::vector<int>>::value << std::endl; } This approach is a lot more concise than the one in my previous answer.
Recommends Joseph Franciscus · 2018-12 · Stack Overflow · on “Friend the non-const templated version of class in C++”
Cppcon 2014 Walter Brown Template Metaprogramming: [this video] He reviews most of the fundamentals of TMP. Though its not for the faint of heart/
Recommends Denis Yaroshevskiy · 2020-05 · Stack Overflow · on “Use C++20 Concepts to force a class to implement a set of methods”
You can, the question is why you want to do this. If your type doesn't do what it's suppose to - you'll get a compilation error, right? If you want to get a compilation error in the same header for some reason, you can do something like: template <typename ...> using void_t = void; // available since c++17 in std template <typename T> using cow_test = void_t< decltype(std::declval<T>().moo(0)), decltype(std::declval<T>().chew(0)) >; class cow { public: void moo(int); void chew(int); }; using test_cow = cow_test<cow>; class cat { public: void meaw(int); void chew(int); }; using test_cat = cow_test<cat>; This will fail in test_cat with: r #1) C++ x86-64 gcc 10.1 Compiler options... 1 <Compilat