Skip to content

Instantly share code, notes, and snippets.

@a2gs
Created May 21, 2019 02:01
Show Gist options
  • Save a2gs/0a27d25431a3eaa514f6d742547eb4c7 to your computer and use it in GitHub Desktop.
Save a2gs/0a27d25431a3eaa514f6d742547eb4c7 to your computer and use it in GitHub Desktop.
Template parameter
#include <iostream>
using namespace std;
void print1(int a[5])
{
for (int i =0; i < 5; i++)
cout << a[i] << endl;
}
template<class T, int N>
void print2(T (&a)[N])
{
for (int i =0; i < N; i++)
cout << a[i] << endl;
}
int main()
{
int x[] = { 1 , 2 , 3};
print1(x); // esperava 5 ?! erro!
print2(x); //ok
}
@a2gs
Copy link
Author

a2gs commented May 21, 2019

Output:
1
2
3
-1077213008
-1218105356
1
2
3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment