-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #161 from zhqu1148980644/master
Change answer ex16.42
- Loading branch information
Showing
3 changed files
with
125 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#include <iostream> | ||
using std::cout; | ||
using std::endl; | ||
|
||
template <typename T> void f(T) | ||
{ | ||
cout << "template 1\n"; | ||
} | ||
|
||
template <typename T> void f(const T *) | ||
{ | ||
cout << "template 2\n"; | ||
} | ||
|
||
template <typename T> void g(T) | ||
{ | ||
cout << "template 3\n"; | ||
} | ||
|
||
template <typename T> void g(T*) | ||
{ | ||
cout << "template 4\n"; | ||
} | ||
|
||
|
||
int main() | ||
{ | ||
int i = 42, *p = &i; | ||
const int ci = 0, *p2 = &ci; | ||
g(42); | ||
g(p); | ||
g(ci); | ||
g(p2); | ||
f(42); | ||
f(p); | ||
f(ci); | ||
f(p2); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#include <iostream> | ||
#include <string> | ||
|
||
using std::string; | ||
using std::cout; | ||
using std::endl; | ||
|
||
template <typename T, typename ... Args> | ||
void foo(const T & t, const Args& ... rest) | ||
{ | ||
cout << "sizeof...(Args): " << sizeof...(Args) << endl; | ||
cout << "sizeof...(rest): " << sizeof...(rest) << endl; | ||
} | ||
|
||
int main() | ||
{ | ||
int i = 0; | ||
double d = 3.14; | ||
string s = "how now brown cow"; | ||
cout << "foo(i, s, 42, d) : " << endl; | ||
foo(i, s, 42, d); | ||
cout << "foo(s, 42, \"hi\") : " << endl; | ||
foo(s, 42, "hi"); | ||
cout << "foo(d, s) : " << endl; | ||
foo(d, s); | ||
cout << "foo(\"hi\") : " << endl; | ||
foo("hi"); | ||
} |