Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function template overloading: Stroustrup example understanding

Tags:

c++

I've browsed a few threads with the same title as this but can't find anything that has helped me figure this out...

The following is a scan of a little example from "The C++ Programming Language", Second Edition, B Stroustrup, section 13.3.2 (page 336).

enter image description here

I don't understand the third sqrt(z) overload resolution. I expected that the resolution would have been sqrt<complex<double>>(complex<double>).

Obv the function double sqrt(double) doesn't fit the bill. But I also thought template<class T>T sqrt(T) couldn't be resolved to sqrt<double>(complex<double>) as this seems to me to imply T has two different resolutions which I would have thought it can't... T must be the same thing throughout it's "scope".

Is there something I have misunderstood and could you point it out? :) Thanks!

like image 421
Jimbo Avatar asked Mar 11 '23 16:03

Jimbo


1 Answers

The second one is a speciallization wich fits the call. The parameter is T, that is, the type in

complex<T>

then the function is

sqrt<double>

Remember that the compiler selects the most speciallized template function.

like image 154
EFenix Avatar answered Apr 06 '23 07:04

EFenix