Don't understand this template with nontype in the book

  • Thread starter Thread starter yungman
  • Start date Start date
  • Tags Tags
    Book
AI Thread Summary
The discussion revolves around understanding a C++ template function example from a book, contrasting it with a simpler version. The first example uses a standard template function with parameters for low and high values, while the second example introduces a more complex syntax involving explicit template instantiation, where the types and values are specified directly in the function call. Participants express confusion over this syntax and its relevance, questioning whether it is a newer feature compared to what is presented in older programming texts. Suggestions are made to search for "function template instantiation" and "explicit instantiation" to better understand the concept. However, some participants find the documentation challenging to comprehend, highlighting the importance of grasping the syntax to effectively navigate programming resources.
yungman
Messages
5,741
Reaction score
294
This is actually very simple. I think the normal way is done like this:
C++:
#include<iostream>
using namespace std;
template<class T2>
auto inRange(const T2& value, int low, int hi)
{
    if ((value <= hi) && (value >= low)) return "It is in range.";
    else return " It is out of range.";
}
int main()
{
    int val1 = 99;
    cout << inRange(val1, 100, 500)  << "\n\n";
    return 0;
}
But this is from the Ivor book:
C++:
#include<iostream>
using namespace std;
template<class T1, int low, int hi>//how does this work
auto inRange(const T1& value)
{
    if ((value <= hi) && (value >= low)) return "It is in range.";
    else return " It is out of range.";
}
int main()
{
    int val2 = 200;
    cout << inRange<double, 100, 500>(val2);//what is this, it not in the function parameter
    cout << "\n\n";
    return 0;
}

I have no idea what the book is doing, what is inRange<double, 100, 500>(val2);? I never seen this before. Is this something newer than my old Gaddis book again?

Thanks
 
Technology news on Phys.org
yungman said:
I have no idea what the book is doing, what is inRange<double, 100, 500>(val2);? I never seen this before. Is this something newer than my old Gaddis book again?
Did you try a web search for "function template instantiation" or similar? I don't believe this is newer than what is presented in the Gaddis book, but his intent wasn't to present every possible detail of all of the features of C++.
Here is some documentation similar to the example you posted - Explicit Instantiation | Microsoft Docs
 
The std::array we got with C++11 is a very useful example of this.
 
Mark44 said:
Did you try a web search for "function template instantiation" or similar? I don't believe this is newer than what is presented in the Gaddis book, but his intent wasn't to present every possible detail of all of the features of C++.
Here is some documentation similar to the example you posted - Explicit Instantiation | Microsoft Docs
Yes, I tried. It would really help IF I know to look for Explict Instantiation. Problem for me is if I don't know the syntax, I don't know what terms to search.

Honestly, I read your link, I have no idea what it is saying. But now I know the term to look for, I'll dig around.

thanks
 
yungman said:
It would really help IF I know to look for Explict Instantiation.
This is something that was covered in previous posts about template functions - explicit instantiation vs. implicit instantiation.
yungman said:
Honestly, I read your link, I have no idea what it is saying.
Well, that's a problem. You can't get very far in programming if you don't understand what is being said in the documentation for some feature of the language.
 
  • Like
Likes Vanadium 50
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top