Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Xinfu Lu
/
MPCS51045
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Pipelines
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
5cc1ba70
authored
May 25, 2018
by
Xinfu L
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Homework-18
parent
b407f0d2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
143 additions
and
0 deletions
hw18/Exploreconcepts.cpp
hw18/conceptSort.cpp
hw18/Exploreconcepts.cpp
0 → 100644
View file @
5cc1ba70
#include<iostream>
#include<vector>
using
namespace
std
;
template
<
class
T
>
concept
bool
EqualityComparable
()
{
return
requires
(
T
a
,
T
b
)
{
{
a
==
b
}
->
bool
;
{
a
!=
b
}
->
bool
;
};
}
void
f
(
EqualityComparable
x
)
{
cout
<<
x
<<
endl
;
}
void
g
()
{}
template
<
typename
T
>
concept
bool
Integer
=
is_integral
<
T
>::
value
;
template
<
typename
T
>
concept
bool
Unsigned_integer
=
Integer
<
T
>
&&
is_unsigned
<
T
>::
value
;
template
<
typename
T
>
class
S
{
};
template
<
Integer
T
>
class
S
<
T
>
{
};
// #1
template
<
Unsigned_integer
T
>
class
S
<
T
>
{
};
// #2
template
<
Integer
T
>
void
f
(
S
<
T
>
);
// A
template
<
Unsigned_integer
T
>
void
f
(
S
<
T
>
);
// B
int
main
()
{
int
i
=
10
;
f
(
i
);
char
s
[]
=
"apple"
;
f
(
s
);
f
(
g
);
return
0
;
}
/*
I tried some examples from the references on the slides and Wikipedia and ran them on wandbox.org.
*/
\ No newline at end of file
hw18/conceptSort.cpp
0 → 100644
View file @
5cc1ba70
#include<iostream>
#include<array>
#include<list>
#include<iterator>
#include<type_traits>
using
namespace
std
;
/*
template<typename T, enable_if_t<is_same_v<typename iterator_traits<T>::iterator_category, random_access_iterator_tag>> * = nullptr>
void
mySort(T beg, T end) {
cout << "Sort->random_access_iterator" << endl;
}
template<typename T, enable_if_t<is_same_v<typename iterator_traits<T>::iterator_category, forward_iterator_tag>> * = nullptr>
void
mySort(T beg, T end) {
cout << "Sort->forward_iterator" << endl;
}
template<typename T, enable_if_t<is_same_v<typename iterator_traits<T>::iterator_category, bidirectional_iterator_tag>> * = nullptr>
void
mySort(T beg, T end) {
cout << "Sort->bidirectional_iterator" << endl;
}
*/
//method #1:
template
<
typename
T
>
concept
bool
ran
=
is_same_v
<
typename
iterator_traits
<
T
>::
iterator_category
,
random_access_iterator_tag
>
;
template
<
typename
T
>
concept
bool
bi
=
is_same_v
<
typename
iterator_traits
<
T
>::
iterator_category
,
bidirectional_iterator_tag
>
;
void
mySort
(
ran
beg
,
ran
end
)
{
cout
<<
"Sort->random_access_iterator"
<<
endl
;
}
void
mySort
(
bi
beg
,
bi
end
)
{
cout
<<
"Sort->bidirectional_iterator"
<<
endl
;
}
/*
//method #2:
template<typename T>
requires(is_same_v<typename iterator_traits<T>::iterator_category, random_access_iterator_tag>)
void
mySort(T beg, T end) {
cout << "Sort->random_access_iterator" << endl;
}
template<typename T>
requires(is_same_v<typename iterator_traits<T>::iterator_category, bidirectional_iterator_tag>)
void
mySort(T beg, T end) {
cout << "Sort->bidirectional_iterator" << endl;
}
*/
int
main
()
{
array
<
int
,
3
>
a
=
{
3
,
2
,
1
};
mySort
(
a
.
begin
(),
a
.
end
());
list
<
int
>
b
=
{
3
,
2
,
1
};
mySort
(
b
.
begin
(),
b
.
end
());
return
0
;
}
/*
I use gcc Head 9.0.0 201805 with "-fconcepts" on wandbox.org to run the above code since I cannot run it on my own MacBook.
I like the concepts-based approach more than enable_if-based approach because it is more generic and also it is more clear.
If I do not define the second concept bool bi, the error messages are following.
Start
prog.cc: In function 'int main()':
prog.cc:67:28: error: cannot call function 'void mySort(auto:1, auto:1) [with auto:1 = std::_List_iterator<int>]'
mySort(b.begin(), b.end());
^
prog.cc:36:1: note: constraints not satisfied
mySort(ran beg, ran end) {
^~~~~~
prog.cc:30:14: note: within 'template<class T> concept const bool ran<T> [with T = std::_List_iterator<int>]'
concept bool ran = is_same_v<typename iterator_traits<T>::iterator_category, random_access_iterator_tag>;
^~~
prog.cc:30:14: note: 'std::is_same_v' evaluated to false
1
Finish
I think it is useful and clear for me.
*/
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment