Wow, I watched for an hour and just realized it was released on KZfaq only an hour ago.
@SamWhitlock19 күн бұрын
36:10 can someone enlighten me about the `void run();` use here? I'm trying to understand what value that adds for the function name lookup
@robertleahy692314 күн бұрын
Speaker here. When you perform qualified name lookup, for example by typing std::swap in your code, you will call a matching overload of swap which is found in exactly the given scope (in this case std). When you perform unqualified name lookup, for example by typing just swap in your code, the search for matching overloads is more interesting. First "regular unqualified name lookup" is performed, which searches for a matching name by looking in enclosing scopes until a match is found. So, for example, if you type quux(a) in a function in the namespace foo::bar regular unqualified name lookup will search: 1. The function body itself, then if no name quux is found 2. The namespace foo::bar, then if no name quux is found 3. The namespace foo, and then if no name quux is found 4. The global namespace Note the "then if no name quux is found." If a name quux is found the search stops immediately. So if you have a quux in foo::bar and in foo only foo::bar::quux will be found, foo::quux will not be considered. Once regular unqualified name lookup finds a match, if the match is a regular function, then another kind of lookup continues populating the "candidate set" (the set of overloads with the correct name which are found by name lookup): Argument dependent lookup. This lookup tries to find matches in the "associated namespaces" of arguments to the function call. So in this case the namespaces containing: - The type of a, and - The template arguments used to instantiate the type of a, if any Are the associated namespaces. All names quux from those namespaces are then added to the candidate set, and then overload resolution occurs. In the example you're talking about we don't want any alternatives which are put in the candidate set by regular unqualified name lookup, we only want names from argument dependent lookup, which means we need to inhibit regular unqualified name lookup somehow. The way we do this is add a "poison pill:" The declaration of a function run (without a definition). Within detail::run when regular unqualified name lookup starts searching for a match it will first consider the calling context, and then detail::run. Once it searches inside detail::run it finds a match, our nullary function declaration, and therefore it: - Ends regular unqualified name lookup, and since it found a regular function - Begins argument dependent lookup Note that the latter is what we actually want. Also note that when we call run we call it unary (with one argument) which means the poison pill we found will get ruled out by overload resolution. Therefore while regular unqualified name lookup does occur we manipulate the results it generates so it's as if it doesn't happen, thereby fulfilling the requirements laid out in text/standardese on the slide before.