CppCon 2014: Scott Meyers "Type Deduction and Why You Care"

CppCon · 116K views · 2014 · 1:09:34

Open on YouTube

C, C++, Rust, Go & systems languagesProgramming language designtalk

Why Stack Overflow recommends it

Stack Overflow users link it when answering questions about auto, template deduction, universal references, and braced initializers, as the clearest explanation of the rules.

Recommended by 11 people across 9 different months, 2014 to 2020.

What it is

Scott Meyers' CppCon 2014 talk on how C++ deduces types in templates, auto variables, universal references, lambdas, and C++14 return type deduction (auto and decltype(auto)). It explains the rules, including the special treatment of braced initializers, and it previews material from his book Effective Modern C++.

What people said

Strong rec kritzikratzi · 2018-01 · Stack Overflow · on “Template deduction ambiguous”

i believe @RichardCritten is right. if you like talks, scott meyers gave an excellent one called "type deduction and why you care" [this video] (it's an hour well invested!)

Recommends Philipp Claßen · 2014-10 · Stack Overflow · on “Why is there a special type deduction rule for auto and braced initializers in C++11/C++14?”

In his CppCon 2014 talke "Type Deduction and Why You Care" [this video] , Scott Meyers raises the question why there is a special rule about auto and braced initializers in the C++11/C++14 standard (his question starts at 36m05s [this video] ). The semantic of auto in combination with a braced-init-list is defined in §7.1.6.4/6. I thought about it and could not come up with a use-case either. The closest thing that I have seen so far is one example where Bjarne Stroustrup used it. In his Cpp 2014 talk "Make Simple Tasks Simple!" [LINK] , he once uses auto to capture initializers (but only as a workaround). Here is the code (part of slide 30, at 37m10s [LINK] ): // auto ss1 = collect({ 1, 2, 3, 4, 5, 6

Recommends akira · 2015-01 · Stack Overflow · on “Postpone class template to its constructor”

You create a creator function which returns "the right thing(tm)": template<typename T1, typename T2> auto make_A(T1 n1, T2 n2)->A<T1, T2> { return A<T1, T2>(n1, n2); } And use it later on like this: auto a = make_A(t1, t2); And that's it. But be aware: auto is a pretty "newish" keyword, you might run into troubles if you run into old compilers. Before refactoring your huge project you should check the lowest compiler you have to support. Further information: Scott Meyers "Type Deduction and Why You Care" [this video] Mike Action "Data-Oriented Design and C++" [LINK] ... this talk is to question your need for "classes because of why not?!"

Recommends Drop · 2015-01 · Stack Overflow · on “convert 'std::initializer_list<int>' to 'int'”

"A braced initializer has no type! A braced initializer has no type! A braced initializer has no type! No type corresponds to braced initializer" (C) Scott Meyers. This is why auto type deduction of brace-initializer with one element fails to follow intuitive rules in C++11 and C++14. It is considered a defect in standard by many programmers. That is why proposal N3922: New Rules for auto deduction from braced-init-list [LINK] exists. Notably: For direct list-initialization: For a braced-init-list with only a single element, auto deduction will deduce from that entry; For a braced-init-list with more than one element, auto deduction will be ill-formed. Not sure if it is a breaking change and

Recommends Drop · 2015-01 · Stack Overflow · on “convert 'std::initializer_list<int>' to 'int'”

There was a good talk on CppCon about quirks of type deduction in C++11/C++14/C++17 by Scott Meyers: link [this video] . Same thing in more details in his new book [LINK] (and there was a free sampler with that chapter as well)

Recommends Zulan · 2016-02 · Stack Overflow · on “Why I can not return initializer list from lambda”

You can return an initializer_list from the function either like that: return std::initializer_list<int>{1, 2}; or auto ret = {1, 2}; return ret; The reason is, that the auto variable declaration uses different rules than the auto return type deduction. The first one has a special rule for this case, and the second one uses plain template type deduction. This is discussed at length in Scott Meyers Effective Modern C++, Item 2. There is also a video [this video] and slides [LINK] from him about the topic.

Recommends Rumburak · 2016-02 · Stack Overflow · on “How to get move, swap and compare count for quicksort in C++”

The problem is not in the way you count. It is here: for(auto data:dataset) // This line is incorrect data.set( rand()); // dataset is not affected by this line You are iterating over values of dataset here. That means that you are modifying copies of the values in dataset . And dataset itself remains unchanged. As a result, there is nothing for your quicksort to do. All the values are default constructed and equal. Thus, dataset is already sorted. No swapping is required. By turning data into a reference, you get what you want: for(auto& data:dataset) // The & is required to take references data.set( rand()); // Now dataset is affected by this line Now you will see reasonable values for you

Recommends Jts · 2016-03 · Stack Overflow · on “How to use template data type inside main function in C++?”

What you are trying to do is not possible. You can only use U within your add function. However, you can do this instead auto result = add(first, second); Or decltype(auto) result = add(first, second); In your case both will do the same. However, they are quite different. To make it short, decltype(auto) will always get you the exact type returned by add , while auto may not. Quick example: const int& test() { static int c = 0; return c; } // result type: int auto result = test(); // result type: const int& decltype(auto) result = test(); If you want to know more about auto, Scott Meyers explains it perfectly: CppCon 2014: Scott Meyers "Type Deduction and Why You Care" [this video]

Recommends Borgleader · 2017-07 · Stack Overflow · on “Using the auto keyword in C++”

`auto Array: PArrays` This makes a copy, you probably want `auto& Array`. Suggested viewing: CppCon 2014: Scott Meyers "Type Deduction and Why You Care" [this video]

Recommends Fureeish · 2018-09 · Stack Overflow · on “What is the correct way to use move semantics in C++?”

@bobl yes, that's correct! This talk [this video] by Scott Meyers (the link should come with timestamp, but it it's broken, it's 16m44s) explains it pretty well. The thing is that `T&&` *does not necessarily always mean rvalue reference* (that's a lie, but it's a comfortable lie). Sometimes, due to *reference collapsing*, it becomes an lvalue reference and `std::forward` 'detects it` and forwards the correct type (either lvalue or rvalue reference) further