CppCon 2014: Walter E. Brown "Modern Template Metaprogramming: A Compendium, Part II"
Why Stack Overflow recommends it
Stack Overflow users link it as the canonical explanation of the void_t idiom whenever they answer questions about detecting members, operators or validity of expressions at compile time.
Recommended by 7 people across 8 different months, 2014 to 2021.
What it is
A CppCon 2014 conference talk by Walter E. Brown, the second part of his template metaprogramming compendium. It demonstrates modern techniques for implementing standard library facilities and introduces void_t, a tiny type_traits addition for detecting well-formed expressions via SFINAE.
What people said
Strong rec Marshall Clow · 2021-04 · Stack Overflow · on “C++ determining at compile time whether a particular operation on one or more types is valid”
You should take a look at void_t , which was introduced in C++14. However, there's nothing in it that shouldn't work in C++11. See Walter Brown's excellent talk about void_t [this video] , starting about 29:00 (but you should watch the whole presentation)
Recommends Tim Seguine · 2014-10 · Stack Overflow · on “void_t "can implement concepts"?”
I was watching the second part of Walter Brown's CppCon2014 talk on template metaprogramming [this video] , during which he discussed the uses of his novel void_t<> construction. During his presentation Peter Sommerlad asked him a question that I didn't quite understand. (link goes directly to the question, the code under discussion took place directly before that) Sommerlad asked Walter, would that mean we actually can implement concepts lite right now? to which Walter responded Oh yeah! I've done it ... It doesn't have quite the same syntax. I understood this exchange to be about Concepts Lite. Is this pattern really that versatile? For whatever reason, I am not seeing it. Can someone explain (
Recommends Piotr Skotnicki · 2014-11 · Stack Overflow · on “C++ template mechanism to get the number of function arguments, which would work for lambdas and normal functions”
@edorado: (1). The *name lookup* finds *primary template*; (2). Compiler searches for specialization (using `void` as second argument); (3). Compiler tries to instantiate the specialization with `decltype((void)&T::operator())`; (4). If the expression in `decltype` is *well-formed*, it gets casted to `void` and hence is treated by compiler as viable *partial-specialization* that can be used (which is designed to work with `operator()`), otherwise, it falls back to *primary template*, and uses that class template for functions. See video [this video]
Recommends vsoftco · 2016-06 · Stack Overflow · on “How can I know the C++ template is a container or a type?”
You can write your own trait and enable_if on it via expression SFINAE [LINK] with multiple overloads. Here is a solution that uses the void_t [LINK] trick [this video] (which will presumably appear in C++17): #include <iostream> #include <type_traits> #include <vector> template<typename ...> using to_void = void; // maps everything to void, used in non-evaluated contexts template<typename T, typename = void> struct is_container : std::false_type {}; template<typename T> struct is_container<T, to_void<decltype(std::declval<T>().begin()), decltype(std::declval<T>().end()), typename T::value_type >> : std::true_type // will be enabled for iterable objects {}; template<typename T> void f(T param, ty
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 [LINK] , Part II [this video] ) 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 Tsvetomir Dimitrov · 2017-02 · Stack Overflow · on “Rvalue reference usage in sample is_copy_asignable implementation”
I am watching Walter Brown's talk on CppCon 2014 'Modern Template Metaprogramming: A Compendium'. In part two, he demonstrates the usage of decltype and declval with a sample implementation of is_copy_assignable. Here is a link to the video, with the slide, showing the source code (rewind a bit if you want to hear his explanations): [this video] Here I have typed his implementation in a sample program: #include <iostream> #include <type_traits> #include <mutex> using namespace std; template <class T> struct my_is_copy_assignable { private: template<class U, class = decltype(declval<U&>() = declval<U const&>())> static true_type try_assignment(U&&); // Why U&& and not for example U? static false_t
Recommends vsoftco · 2017-06 · Stack Overflow · on “Why this SFINAE snippet is not working in g++, but working in MSVC?”
SFINAE doesn't work here, as the class is already instantiated with T = int in do_have_size<int>::type . SFINAE works only for a list of template function candidates, in your case you'll get a hard error since in the instantiation do_have_size<int>::type the member function template <typename = decltype(std::declval<int>().size())> static std::true_type check(T); is surely ill-formed for int . The static std::false_type check(...); won't ever be considered. So gcc is right here in rejecting your code and MSVC2017 should not accept the code. Related: std::enable_if : parameter vs template parameter [LINK] and SFINAE working in return type but not as template parameter [LINK] One solution is t
Recommends jwd · 2020-04 · Stack Overflow · on “Using std::conditional with is_class<int>, getting compile error”
Thanks! Small point for future searchers: `std::void_t` is C++17 (though pretty trivial to implement in my C++14 environment). And a good [this video] on the subject.
Cites Trevor Hickey · 2014-11 · Stack Overflow · on “What are the 15 classifications of types in C++?”
During a CppCon2014 conference talk [this video] by Walter E. Brown, he states that there are 15 classifications of types in C++ that the standard describes. "15 partitions of the universe of C++ types." " void is one of them." -- Walter E. Brown. What are the other 14? While digging through the standard, I found the following: // 20.11.4.1 primary type categories: template <class T> struct is_void; template <class T> struct is_integral; template <class T> struct is_floating_point; template <class T> struct is_array; template <class T> struct is_pointer; template <class T> struct is_lvalue_reference; template <class T> struct is_rvalue_reference; template <class T> struct is_member_object_pointer
Cites 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 [LINK] , Part II [this video] ) 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