Templates string constructor | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Templates string constructor

I don't understand because with template<class Iter> string(Iter begin, Iter end) constructor of the String Class Library I don't need to use the keyword "template<class Iter>", why??

22nd Sep 2020, 2:35 PM
Carlos Caminero
1 Answer
+ 2
It's called template deduction. A template function should know what types should be used before starting the function. But we don't have to specify every template arguments before using. If there's missing. The compiler will check from the function parameters and get their types. In a nutshell, if we have a function: template<class T> void func<T>(T x). You can call it with: func(10)., rather than func<int>(10) Because the compiler gets the type of parameter you passed, which is int.
29th Sep 2020, 11:41 AM
你知道規則,我也是
你知道規則,我也是 - avatar