-
Notifications
You must be signed in to change notification settings - Fork 0
/
type_traits-04.cc
53 lines (44 loc) · 1.14 KB
/
type_traits-04.cc
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
42
43
44
45
46
47
48
49
50
51
52
53
//
// Program
// Type traits - An improved version of type_traits-03
// The main theme is to use using for value management.
//
// Compile
// g++ -Wall -Wextra -pedantic -std=c++17 -o type_traits-04 type_traits-04.cc
//
// Execution
// ./type_traits-04
//
#include <iostream>
// A structure takes a bool value
// and assign to value
template<bool val>
struct is_const_base {
// static: To access it without creating object of struct
// const: Once assigned, don't change its value
// value: Recommended variable name to expose value
static const bool value = val;
};
// Define types rather using struct name
using const_false_type = is_const_base<false>;
using const_true_type = is_const_base<true>;
// Extend is_const_base and have false value
// for everything
template<typename T>
struct is_const: const_false_type {
};
// Specialization: Extend is_const_base and have true value
// if given element is const qualified.
template<typename T>
struct is_const<const T>: const_true_type {
};
//
// Entry function
//
int main() {
{
static_assert(is_const<const int>::value);
static_assert(not is_const<int>::value);
}
return 0;
}