forked from Mooophy/Cpp-Primer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
41 lines (37 loc) · 1.14 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/***************************************************************************
* @file main.cpp
* @author Alan.W
* @date 02 Feb 2014
* @remark This code is for the exercises from C++ Primer 5th Edition
* @note
***************************************************************************/
//
// Exercise 16.7:
// Write a constexpr template that returns the size of a given array.
//
// Exercise 16.8:
// In the “Key Concept” box on page 108, we noted that as a matter of habit
// C++ programmers prefer using != to using <. Explain the rationale for
// this habit.
//
// The reason is that more class defines "!=" rather than "<". Doing so can
// reduce the number of requirement of the class used with a template class.
//
#include <iostream>
#include <vector>
#include <list>
#include <string>
template<typename T, unsigned size>
constexpr unsigned getSize(const T(&)[size])
{
return size;
}
int main()
{
std::string s[] = { "sss" };
std::cout << getSize(s) << std::endl;
char c[] = "s";
std::cout << getSize(c) << std::endl;
// the output is 2, as '\0' is added at the end of the array
return 0;
}