-14

I'm reading Bjarne Stroustrup's book on C++ and he uses things like vector<int> or complex<double>. What does it mean when a data type like int is between a < and > sign?

Tried Googling but it won't recognize my < or > enter image description here

enter image description here

cchoe1
  • 325
  • 2
  • 15
  • Google --- > C++ Templates – sgarizvi May 12 '18 at 21:46
  • Try the book list from the C++ FAQ: https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282 – Richard Critten May 12 '18 at 21:47
  • 17
    Or actually read Stroustrup's book with a modicum of attention. –  May 12 '18 at 21:47
  • Templates are only mentioned in the ToC and then they're actually covered in 3.4. He uses templates in examples in Chapter 2. I was trying to actually understand what was happening in each example as I go yet he doesn't cover templates until Chapter 3. – cchoe1 May 12 '18 at 21:49
  • 1
    What do you expect us to say that the book you posted did not say? – Galik May 12 '18 at 22:28

1 Answers1

4

They're templates.

Function templates are special functions that can operate with generic types. This allows us to create a function template whose functionality can be adapted to more than one type or class without repeating the entire code for each type.

for example:

template <class C>
C add (C a, C b) {
  C result = a + b;
  return (result);
}

int a = 1;
int b = 2;

add<int>(a, b); //returns 3

float c = 1.5
float d = 0.5

add<float>(c, d) // returns 2.0
Invariance
  • 1,027
  • 1
  • 7
  • 15
  • 2
    Thanks for actually answering my question. I guess I should have read through the entire book before asking a single question – cchoe1 May 12 '18 at 21:55