Mindless repetition is an ideal job for a computer, hence a function template:. Unlike template classes , template functions usually do not need to be explicit about the parameters over which they are instantiating.
The compiler can usually determine them automatically. Sometimes, you do want to be explicit about the types used. When you call a function template, the compiler tries to deduce the template type. Most of the time it can do that successfully, but every once in a while you may want to help the compiler deduce the right type — either because it cannot deduce the type at all, or perhaps because it would deduce the wrong type. In this case the compiler cannot deduce the template parameter types when the function is called.
To call this function with T being an int or a std::string , you could say:. Now if you want to force the actual arguments to be promoted before the compiler deduces the template type, you can use the above technique.
Of course you could also promote the parameter explicitly, such as either g long 42 or even g 42L , but that ruins the example. Another time when you must specify the types is when the function takes two parameters of the same type, but you give it two different types. A parameterized type is a type that is parameterized over another type or some value. In other words, is the observable behavior different in some substantive way?
For example, if the code for int inserts something into a container and sorts the result, but the code for std::string removes something from a container and does not sort the result, those two functions ought not to be an overloaded pair — their observable behavior is different so they ought to have different names. One way to implement the above is via template specialization.
Instead of a switch -statement, you end up breaking up the code into separate functions. The first function is the default case — the code to be used when T is anything other than int or std::string :.
The compiler will automagically select the correct specialization when it sees which T you are using. One of several ways I personally use template specialization is for stringification. I usually use a template to stringify various objects of various types , but I often need to specialize the code for stringifying certain specific types. For instance, when stringifying bool s I prefer "true" and "false" over "1" and "0" so I use std::boolalpha when T is bool.
Also I often prefer floating point output to contain all the digits so I can see very small differences, etc. The end result usually looks something like this:. Conceptually they all do the same thing: stringify the parameter. That means the observable behavior is consistent, therefore the specializations do not confuse callers. However the details for implementing that observable behavior is slightly different for bool and floating point types, so template specialization is a good approach.
If you blindly applied the advice from the FAQ on template specialization , you would end up duplicating all that code before and after the pseudo-switch statement. The main foo function would be a simple template — no specializations. It all gets figured out automatically. This is a quality-of-implementation issue so your results may vary. However there is usually no slow-down at all.
If anything, the templates might affect the speed of compilation slightly, but once the types are resolved by the compiler at compile-time, it will typically generate code that is just as fast as with non-template functions, including inline-expanding appropriate functions, etc. Function templates participate in name resolution for overloaded functions, but the rules are different. For a template to be considered in overload resolution, the type has to match exactly.
If the types do not match exactly, the conversions are not considered and the template is simply dropped from the set of viable functions. If all you want to know is how to fix this situation, read the next two FAQs. But in order to understand why things are the way they are, first accept these facts:. The important thing to note about non-type parameters is, they must be const. The compiler must know the value of non-type parameters at compile time.
In below program, if we replace or 25 with a variable, we get a compiler error. Please see this. See Template Metaprogramming You may also like to take a quiz on templates. Java also supports these features. Java calls it generics. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. Skip to content. Change Language. Related Articles. Table of Contents. Save Article. Improve Article. Like Article. This approach implements compile-time polymorphism over run-time polymorphism.
To learn more about programming and other related concepts, check out the courses on Great Learning Academy. Remember Me! Great Learning is an ed-tech company that offers impactful and industry-relevant programs in high-growth areas. Know More. Sign in. Log into your account. Forgot your password? Password recovery. Recover your password. Introduction to Command Line Arguments in C? Please enter your comment!
0コメント