Whats the need / advantage of default template type | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 4

Whats the need / advantage of default template type

Hi I just checked code for template default. I tried code without that default. Output is same. Can someone let me know difference between template <typename T> And template <typename T = int> https://code.sololearn.com/c8JFCxsjWS0T/?ref=app

11th Dec 2022, 6:34 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
8 Réponses
+ 6
Ketan Lalcheta You don't see a difference between the two as in all cases, the type T is successfully deduced by the compiler because of the argument passed to it. Keep in mind that int is just a default argument. Template deduction will still proceed in the same manner, i.e. when an argument is passed, T will be deduced from that argument. Since the type deduced in display(2.2) is double, it displays 2.2 The default argument is only used when there is no other way for the template T to be deduced. This example might help you understand the difference better https://code.sololearn.com/cSn29Y102HGL/?ref=app
11th Dec 2022, 9:30 AM
XXX
XXX - avatar
+ 6
In the last example, the function is called without specifying the data type for T, so the default int data type is used.. https://code.sololearn.com/c8m4jW8b726H/?ref=app
11th Dec 2022, 8:00 AM
Sadaam Linux
Sadaam Linux - avatar
+ 5
To use that 👆 function, you would need to specify the data type for the T placeholder when you call the function. example: int result1 = add(1, 2); // result1 will be 3 double result2 = add(1.5, 2.5); // result2 will be 4.0 If you want to use the int data type as the default for the T placeholder, you can modify the function declaration to use the template <typename T = int> syntax: template <typename T = int> T add(T a, T b) { return a + b; } This allows you to use the function without explicitly specifying the data type every time. For example: int result1 = add(1, 2); // result1 will be 3 double result2 = add(1.5, 2.5); // result2 will be 4.0 string result3 = add("Hello ", "World"); // result3 will be "Hello World"
11th Dec 2022, 7:58 AM
Sadaam Linux
Sadaam Linux - avatar
+ 4
The template <typename T> syntax is used to declare that a function or class is a template, with the placeholder T representing the type of data that the function or class will be working with. The template <typename T = int> syntax is similar, but it specifies a default data type of int for the T placeholder. This means that if a data type is not specified when the function or class is used, int will be used by default. This can be useful when the function or class is expected to work with int data most of the time, as it allows you to use the function or class without explicitly specifying the data type every time. an example of a simple function template that adds two values of any data type: template <typename T> T add(T a, T b) { return a + b; }
11th Dec 2022, 7:54 AM
Sadaam Linux
Sadaam Linux - avatar
+ 3
Sadaam Linux "This means that the function will treat the input values as int values and add them together even though they are actually strings" Maybe I'm not understanding you correctly, but the function did not treat the input values as int. The template T was correctly deduced to be a string. The default type is used only if there's no other way to way to deduce the type T.
11th Dec 2022, 9:23 AM
XXX
XXX - avatar
+ 2
Thanks Sadaam Linux and XXX ...
11th Dec 2022, 11:41 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
XXX Not to hijack the thread but following because templates are still new to me and it's an interesting question. So, if I'm getting this right, in this case: template<class T=string> if you pass an int or float to it essentially the T=string default is ignored as the template will correctly deduce the argument. You can also, for example, pass a string to T=int. It seems to be the case as in this ex. If the int and float weren't correctly interpreted then the float multiplication would return an error. https://code.sololearn.com/csDnnQqg8Rda/?ref=app
11th Dec 2022, 10:51 AM
Scott D
Scott D - avatar
+ 1
Scott D Yes, T=string means nothing when T can be deduced to some other type. string will only be used when T cannot be deduced.
11th Dec 2022, 12:04 PM
XXX
XXX - avatar