Don't understand this template with nontype in the book

  • Thread starter yungman
  • Start date
  • Tags
    Book
In summary: The documentation explains what explicit instantiation is and how it works. If you don't understand it, you should ask more specific questions. But now I know the term to look for, I'll dig around.That's great. Keep learning and practicing, and you'll become more familiar with these concepts.
  • #1
yungman
5,755
293
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
  • #2
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
 
  • Like
Likes yungman
  • #3
The std::array we got with C++11 is a very useful example of this.
 
  • Like
Likes yungman
  • #4
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
 
  • #5
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

FAQ: Don't understand this template with nontype in the book

What is a nontype in the context of this book?

A nontype in the context of this book refers to a type of data or object that cannot be classified into a specific type or category.

Why is understanding this template important for scientists?

Understanding this template is important for scientists because it allows them to accurately classify and analyze data, which is essential for producing accurate and meaningful results in their research.

Can you provide an example of a nontype in scientific research?

One example of a nontype in scientific research could be a sample that does not fit into any existing categories or classifications, requiring scientists to create a new category for it.

How can not understanding this template affect scientific research?

Not understanding this template can have a significant impact on scientific research as it can lead to incorrect data analysis and interpretation, which can ultimately result in faulty conclusions and findings.

Are there any resources available to help scientists better understand this template with nontype?

Yes, there are various resources such as textbooks, online articles, and workshops that can provide further explanation and guidance on understanding this template with nontype in the context of scientific research.

Similar threads

Back
Top