Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #68221
| From | Ian Collins <ian-news@hotmail.com> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: Template argument deduction mystery |
| Date | 2019-09-05 07:16 +1200 |
| Message-ID | <gtagsnFnafoU3@mid.individual.net> (permalink) |
| References | <qkofo9$9mo$1@adenine.netfront.net> |
On 05/09/2019 01:55, Juha Nieminen wrote:
> In C++17 you can do this:
>
> //-----------------------------------------------------
> #include <iostream>
> #include <algorithm>
> #include <functional>
>
> int main(int argc, char** argv)
> {
> double values[5] = { 1.1, 2.2, 3.3, 4.4, 5.5 };
>
> // Notice this:
> std::sort(values, values+5, std::greater());
>
> for(double v: values) std::cout << v << " ";
> std::cout << "\n";
> }
> //-----------------------------------------------------
>
> You don't actually need to specify the template parameter of std::greater.
> It will be automatically deduced as 'double'. That's really nice.
>
> But wait... How?!?
>
> There's nothing telling std::greater that the template parameter should be
> of type double. It's not taking any constructor parameters or anything.
> And the declaration of std::sort() is ostensibly like this:
>
> template<class RandomIt, class Compare>
> void sort(RandomIt first, RandomIt last, Compare comp);
>
> so there's nothing there telling it that it should be double either.
>
> So how?
Magic? I assume it's related to the changes in C++17 which extend
template argument deduction to template classes as well as functions.
All of the information is there, given the signature of Compare is bool
comp(const Type1 &a, const Type2 &b) and Type1 and Type2 can be deduced
from RandomIt.
--
Ian.
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Template argument deduction mystery Juha Nieminen <nospam@thanks.invalid> - 2019-09-04 13:55 +0000
Re: Template argument deduction mystery Ian Collins <ian-news@hotmail.com> - 2019-09-05 07:16 +1200
Re: Template argument deduction mystery Bonita Montero <Bonita.Montero@gmail.com> - 2019-09-05 03:03 +0200
Re: Template argument deduction mystery Juha Nieminen <nospam@thanks.invalid> - 2019-09-05 21:50 +0000
Re: Template argument deduction mystery Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo> - 2019-09-05 20:04 -0400
Re: Template argument deduction mystery Chris Vine <chris@cvine--nospam--.freeserve.co.uk> - 2019-09-06 14:55 +0100
Re: Template argument deduction mystery Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo> - 2019-09-06 23:56 -0400
Re: Template argument deduction mystery Chris Vine <chris@cvine--nospam--.freeserve.co.uk> - 2019-09-07 15:25 +0100
Re: Template argument deduction mystery Juha Nieminen <nospam@thanks.invalid> - 2019-09-07 18:55 +0000
Re: Template argument deduction mystery Bo Persson <bo@bo-persson.se> - 2019-09-07 21:28 +0200
Re: Template argument deduction mystery Ian Collins <ian-news@hotmail.com> - 2019-09-08 12:29 +1200
Re: Template argument deduction mystery Chris Vine <chris@cvine--nospam--.freeserve.co.uk> - 2019-09-07 23:06 +0100
Re: Template argument deduction mystery Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo> - 2019-09-07 18:35 -0400
csiph-web