Using the Model Syntax for Arguments

advertisements

Please note the std::get function syntax below:

std::tuple <int, double, long &, const char *> bar(18, 6.5, 12, "Hello!");
cout << std::get<2>(bar); // Print the tuple’s third element.

I'm not familiar with this syntax ...
Is this using template syntax for passing arguments?
If yes, please provide a simple example of how std::get works.


You can pass stuff as arguments in a template parameter just like anything else, you're probably familiar with passing types as arguments like so:

template<typename T>
void f() {}

and calling the function like this: f<int>().

This is the same concept except it's extended to be a concrete type.

Example:

#include <iostream>

template<size_t N>
void f() {
    std::cout << N << '\n';
}

int main() {
    f<10>();
    f<11>();
    f<100>();
}

Output:

10
11
100

So instead of passing a type, you are passing a compile-time value of the type specified.