Entries:
Comments:
Posts:

Loading User Information from Channel 9

Something went wrong getting user information from Channel 9

Latest Achievement:

Loading User Information from MSDN

Something went wrong getting user information from MSDN

Visual Studio Achievements

Latest Achievement:

Loading Visual Studio Achievements

Something went wrong getting the Visual Studio Achievements

Stephan T. Lavavej: Core C++, 2 of n

Download

Right click “Save as…”

In part 2, STL will teach us all about Template Argument Deduction. Template arguments are deduced when a call is made to a template function, but some or all template arguments are omitted. The compiler will attempt to deduce the intended template arguments. In most cases, this works as expected. If it does not, a compilation error occurs, in which case you should specify the template arguments explicitly. Now, let's see what Stephan has to say about this.

Tune in. Learn.

See part 1: Name Lookup

Tags:

Follow the Discussion

  • Nice to see another classic Stephan lecture - and with his own banner artwork too Smiley

    I know Andrei recently covered variadic templates, but I'd love to hear Stephan's perspective on the topic as it's reasonably complex and would I think benefit from multiple view points.

  • "that probably sounded like Klingon"

    LOL, actually it sounded like a template compiler error. is that the same as Klingon?

    D830?

  • IvanIvan

    Hehe, STL is gonna get fired, we all know what language matches that terrible horrible comment regarding inheriting from Object targets. :P
    Joking aside another cool lecture by STL.

  • PhilhippusPhilhippus

    Not that I want to give you any ideas but I can't believe these lectures are free!

    Now that we're clear on template argument deduction, and next is template overload resolution, I was wondering where variadic templates fit into all this. I didn't catch whether they were included in the standard after all or not...?

  • STLSTL

    dot_tom: I've written a compile-time array sorter that I'll be doing a video about at an unspecified point in the future.

    Ivan: I was actually referring to pre-STL C++, when nobody knew any better.

    Philhippus: Variadic templates are definitely part of C++11, it's just that they aren't implemented in VC11 RTM (much less VC10 SP1 which is what I currently have on my laptop).  At that unspecified point in the future when I can show off a Project Ellipsis compiler, I will do so.

  • IvanIvan

    @STL
    that thing with lambdas and TAD that dont work without Identity trick: to me it seems more like a limitation, not a sensible thing to define in a standard. Am I right?
    BTW
    might seem trivial, but do you know why C++11 to_string is not templated on char type? There is a to_wstring, but no char16_t or char32_t versions, and I guess for writing generic code(that doesnt even use char16_t or char32_t, just char and wchar_t) that is a big pain.
    Trivia: googling project Elipsis compiler: first result is your comment. :)

  • If template argument deduction does not allow any conversions, how come "peppermint" is converted from const char[11] to const char* before being matched with the const T* overload?

  • KennyKenny

    Thanks a lot for the wonderful lecture. As much as I watch your lectures series, I found that they are much valuable.
    But the time gap between the last one "1 of n" and this lecture was too large. I hope the next video to be uploaded sooner.

    And one question about templates, some times when I write inline template member functions for my classes, I get complicated link errors, But making them non-inline solves the problem, since there is no place for actually putting a sample code here... is there any common mistake that causes this.

  • CharlesCharles Welcome Change


    But the time gap between the last one "1 of n" and this lecture was too large. I hope the next video to be uploaded sooner.

    STL writes code for a living (as opposed to doing Channel 9 videos...). We make these as his schedule permits - and that's the way it should be! I have a feeling we'll get the next one out sooner, but that doesn't mean the one after that will come as soon. It's amazing that STL finds any time at all do these in depth lectures (he has to do some amount of prep before he comes to the studio for filming).

    C

  • JohnathonJohnathon

    I have downloaded all of Stephan's lectures. And I find myself continuing to do so even with this core series. He's always able to somehow show you something you may have not already known, or something you've been using but didn't grasp fully. There is no limit to the amount of appreciation I have for what Stephan does for the community of c++ developers(the new and old developers). The fact his lecture posts come in large time spans apart should not be an issue, it is after all free video lecture series on a caliber of pedigree contiguous with a very expensive learning series.

  • STLSTL

    Ivan: It is a limitation, but I argue that it is an inherent and unavoidable one, instead of C++ being arbitrarily cruel to us for no good reason. Template argument deduction runs before overload resolution, and overload resolution considers implicit conversions - it would be very confusing if template argument deduction also considered implicit conversions. As I mentioned in the video, it is a good thing that template argument deduction plays a "relatively simple" type matching game. It makes the vast majority of cases easier to reason about. The std::function example I presented was quite unusual - which is why I presented it, to demonstrate how programmers skilled with the Core Language can express things that are beyond what other programmers can express.

    Ivan> might seem trivial, but do you know why C++11 to_string is not templated on char type?

    to_string() and to_wstring() are specified in terms of sprintf() and swprintf(), which don't handle arbitrary characters.

    Ivan> Trivia: googling project Elipsis compiler: first result is your comment. Smiley

    I make up phrases all the time. (Project Ellipsis's slogan: Imagine a highly miniaturized Death Star. THREE OF THOSE.) When I mention SCARY iterators I actually have to point out that that phrase was not my doing.

    NotFredSafe> If template argument deduction does not allow any conversions, how come "peppermint" is converted from const char[11] to const char* before being matched with the const T* overload?

    In the video, I mentioned that template argument deduction matches function parameters to function arguments with *almost* no wiggle room. This wiggle room does allow arrays to decay to pointers. The Standardese is N3376 14.8.2.1 [temp.deduct.call]/2:

    "If P is not a reference type: — If A is an array type, the pointer type produced by the array-to-pointer standard conversion (4.2) is used in place of A for type deduction; otherwise, — If A is a function type, the pointer type produced by the function-to-pointer standard conversion (4.3) is used in place of A for type deduction; otherwise, — If A is a cv-qualified type, the top level cv-qualifiers of A's type are ignored for type deduction."

    Kenny: I'm glad you like my videos. As Charles mentioned, I am a "dual-use technology" and as much as I'd like to film videos and write blog posts all day, working on the STL itself is my overriding priority. I also do other things from time to time. In this case, I filmed Part 1 on May 8, then attended C++Now! 2012 from May 13-18. The week after that, I was on vacation - I grew up in Colorado and my mom still lives there, so when I go to BoostCon/C++Now! it's easy to visit her afterwards. When I got back, I had to fix some bugs for VC11 RTM. Then I was able to film Part 2 on June 21. The editing/encoding/uploading pipeline takes a reasonably constant amount of time, so any gaps between videos are always the result of my schedule, not C9's excellent team.

    Kenny> And one question about templates, some times when I write inline template member functions for my classes, I get complicated link errors, But making them non-inline solves the problem, since there is no place for actually putting a sample code here... is there any common mistake that causes this.

    Without an example, I can't say. Upload a minimal example to your SkyDrive or pastebin or whatever, and I'd be happy to take a quick look (you should include the whole code, the whole command line used to compile it, and the whole output so I can verify that you're getting what I'm getting).

    The usual gotcha is people thinking that they can separately compile templates, and getting linker errors as a result, but what you describe sounds different.

    Johnathon: Thank you, that means a lot to me.

  • I don't know if this is too basic or not, but how about making an episode about best practices for parameter passing in C++11?

    Like, when do you pass by value, by lvalue reference, by lvalue reference to const, by rvalue reference, by rvalue reference to const, by pointer or by pointer to const? When should you prefer passing iterators instead? Is it a good idea to have iterators as members? Or should they just be used inside a function? How do you pass stuff into members? Should move-only types be passed by rvalue reference or or by value? Should we pass shared pointers by lvalue reference to const, by rvalue reference to const or by value?

    What do we do in generic code? How do we decide what the best passing mode for T is? (Doesn't Boost have a meta-function for this?) What's the difference between ConreteType&& and TemplateParameter&&? Related: what does auto&& mean, and when should I use it?

    I think it would be really helpful, especially for beginners, if you could dissect these issues in the next episode.

  • Ankur ShahAnkur Shah

    Hi Stephan,

    I am fan of your videos. These are very knowledge able that you provide.
    Can you please provide some Videos on MFC architecture and WIN32 APIs

    Thanks
    Ankur Shah

  • IvanIvan

    @Ankur
    tbh I think that STL is expert in C++ core language and STL and that he doesnt have the same passion for Win32 and MFC as he does for C++ core language and standard library. But then again I could be wrong.

  • @Ankur Shah:

    http://msdn.microsoft.com/it-it/visualc/bb496952.aspx

  • Kim GrasmanKim Grasman

    Do you actually use a templated overload to solve the problem of const char* vs std::basic_string?

    I see that it solves the problem of different basic char types, but are there techniques to prevent things like:

    double d = 3.14;
    meow(&d);

    ?

    I've always gone w/ plain overloads for char/wchar_t, but it does get repetitive...

    Thanks for a great lecture, illuminating as always!

  • rhalbersmarhalbersma

    @Stephan
    Thanks a lot for these fantastic lectures on STL, Advanced STL and now Core C++. The most important thing I learnt is not be afraid of reading the Standard. Can't wait for the great overload resolution episode!

  • @NotFredSafe: Take a look a the following three-parter:

    http://codesynthesis.com/~boris/blog/2012/06/19/efficient-argument-passing-cxx11-part1/
    http://codesynthesis.com/~boris/blog/2012/06/26/efficient-argument-passing-cxx11-part2/
    http://codesynthesis.com/~boris/blog/2012/07/03/efficient-argument-passing-cxx11-part3/

    Incidentally, STL, would you agree with the above guidelines (summarized in part 3)?

     

  • STLSTL

    NotFredSafe: I've seen a lot of confusion in that area, so that's an excellent idea for an episode - thanks. I'm still planning to cover overload resolution in Part 3, but I've made a note to myself for future parts. Quick notes - const rvalue references should almost never be used. I know of precisely one use for them, in a relatively obscure scenario. Also, I covered auto&& briefly in my GoingNative 2012 presentation.

    Ankur Shah: I know virtually nothing about MFC, and only scattered things about the Windows API. I am focused on the C++ Standard Library, which also means that I learn a lot about the Core Language and the C Standard Library (but not as exhaustively as a compiler dev or CRT maintainer).

    Kim Grasman> Do you actually use a templated overload to solve the problem of const char* vs std::basic_string?

    Yes, that's literally how <regex> is specified to work.

    > I see that it solves the problem of different basic char types, but are there techniques to prevent things like: double d = 3.14; meow(&d);

    What prevents sort(RanIt, RanIt) from being fed bidirectional iterators, or strings?

    The answer is nothing. It will explode during template instantiation (unless you have a particularly helpful Standard Library implementation that's full of static_asserts).

    > I've always gone w/ plain overloads for char/wchar_t, but it does get repetitive...

    That won't handle other character types (notably C++11's char16_t/char32_t).

    rhalbersma: I'm glad you like them. One of my goals has definitely been to demystify the Standard (as much as possible, anyways). Learning how to read Standardese is an acquired skill, but one that pays off again and again.

    Matt_PD> Incidentally, STL, would you agree with the above guidelines (summarized in part 3)?

    1. "The only catch is that it works at compile time. So what seems to be missing is a similar mechanism that works at runtime."

    Wow, this is confused. lvalueness versus rvalueness is a compile-time concept to begin with. Avoiding runtime logic is the whole point. (Branches, indirections, etc. aren't free even when everything is predicted and cached.)

    The other problem here is that it misidentifies perfect forwarding's catch. The catch is that it doesn't constrain the argument types at all, whereas overloading on const X&/X&& or taking X does constrain the argument types. This can be dealt with by using static_asserts or enable_if (at the cost of more work, of course).

    A potential catch (also not mentioned) is that perfect forwarding implies templates implies header-only code, but it is not an especially big deal (its major impact would be on pimpls).

    Part 1 is otherwise fairly clueful.

    Part 2 introduces runtime logic. This is an abomination and should be avoided at all costs.

    Part 3's guidelines are reasonableish.

  • @STL: Thanks for the answer (and the great lectures)! Now I'm looking forward to see the next episode even more Smiley

     

  • Wondering What ThenWondering What Then

    &#38;&#35;34&#59;...you can tell I don&#38;&#35;39&#59;t type in the IDE.&#38;&#35;34&#59;

  • @STL: the compile time array sorter sounds like fun, are you using variadics to enable n-item initialization?

  • STLSTL

    I use variadic templates for the sorting, but the final initialization is just aggregate initialization (from an expanded parameter pack).

  • PeterPeter

    @STL: Is there any plan to implement stdext::SmallVector, which is the same as std::vector, but it has an extra template parameter to specify the size for inlined container item space?
    I can usually approximate how many items will my std::vector use, but there are rare situations where I need more. SmallVector would speed up my code by preventing allocation and reducing memory usage.

    I have found this idea in the LLVM source code:
    http://llvm.org/docs/doxygen/html/classllvm_1_1SmallVector.html

    Also there are SmallString, SmallSet, SmallMap too!

  • alexalex

    thanks :)
    but can you upload these videos in mkv or webm formats ?
    they size are very much but quality is low !

  • philippphilipp

    Maybe I haven't watched enough of your videos, but what's it with this catchphrase "by default - the two greatest words in the english language"?

  • [1F13] Deep Space Homer

    Scientist: [resigned] Well, Homer, I guess you're the winner by default.

    Homer: Default? Woo hoo! The two sweetest words in the English language: de-fault! De-fault! De-fault!

    [assistant clubs him]

  • STLSTL

    Peter> Is there any plan to implement stdext::SmallVector

    I believe that's been talked about on the Boost mailing list, but not on the Standardization Committee's. My general position is to avoid implementing non-Standard extensions, unless they are clearly destined to make it into the Standard (e.g. <filesystem>) or there is a critical need.

    philipp: Yep, it's a Simpsons reference, as correctly cited by NotFredSafe. When I have the chance to say something ridiculous, I take it :->

  • CharlesCharles Welcome Change

    thanks Smiley
    but can you upload these videos in mkv or webm formats ?
    they size are very much but quality is low !

    We do provide a webm version for all of our videos. Is it not working for you (is the quality not good enough)?

    http://media.ch9.ms/ch9/946b/5538634e-0f05-42e3-b43a-448579ac946b/STLCCSeries2.webm

    C

     

  • STLSTL

    Part 3 is up: http://channel9.msdn.com/Series/C9-Lectures-Stephan-T-Lavavej-Core-C-/Stephan-T-Lavavej-Core-Cpp-3-of-n

Remove this comment

Remove this thread

close

Comments Closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.