Don't understand this template with nontype in the book

  • Thread starter Thread starter yungman
  • Start date Start date
  • Tags Tags
    Book
Click For Summary

Discussion Overview

The discussion centers around understanding a specific template syntax in C++ as presented in a book, particularly focusing on the use of non-type template parameters. Participants are exploring the differences between traditional template usage and the example provided in the book.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Conceptual clarification

Main Points Raised

  • One participant presents a traditional template function and questions the syntax used in the book's example, specifically the use of non-type parameters in the template declaration.
  • Another participant suggests searching for "function template instantiation" to better understand the concept, indicating that the book's approach is not necessarily new.
  • A participant mentions the std::array from C++11 as a relevant example of template usage.
  • There is a discussion about the difficulty of understanding documentation without prior knowledge of specific terms, highlighting the challenge of learning new syntax.
  • One participant expresses frustration with the documentation, indicating a lack of clarity in understanding explicit instantiation versus implicit instantiation.

Areas of Agreement / Disagreement

Participants express varying levels of understanding regarding the template syntax and documentation. There is no consensus on the clarity of the book's example or the documentation provided.

Contextual Notes

Participants note limitations in their understanding of specific C++ terminology and syntax, which affects their ability to search for relevant information effectively.

yungman
Messages
5,741
Reaction score
291
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;
    count << 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;
    count << inRange<double, 100, 500>(val2);//what is this, it not in the function parameter
    count << "\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
 
  • Like
Likes   Reactions: yungman
The std::array we got with C++11 is a very useful example of this.
 
  • Like
Likes   Reactions: yungman
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   Reactions: Vanadium 50

Similar threads

  • · Replies 35 ·
2
Replies
35
Views
4K
  • · Replies 39 ·
2
Replies
39
Views
5K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
63
Views
5K
  • · Replies 23 ·
Replies
23
Views
2K
Replies
12
Views
2K
  • · Replies 17 ·
Replies
17
Views
2K
Replies
73
Views
6K
  • · Replies 31 ·
2
Replies
31
Views
3K
  • · Replies 52 ·
2
Replies
52
Views
4K