-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
121 additions
and
18 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
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,8 @@ | ||
#include <list> | ||
#include <algorithm> | ||
|
||
int main() { | ||
std::list<int> l = {1, 2, 3}; | ||
// std::sort(l.begin(), l.end()); // tons of compile error | ||
return 0; | ||
} |
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,66 @@ | ||
#include <iostream> | ||
#include <string> | ||
#include <vector> | ||
#include <list> | ||
|
||
using namespace std; | ||
|
||
template<typename T> | ||
concept bool Stringable = requires(T a){ | ||
{a.to_string()} -> string; | ||
}; | ||
|
||
template<typename T> | ||
concept bool HasStringFunc = requires(T a){ | ||
{ to_string(a) } -> string; | ||
}; | ||
|
||
struct Person { | ||
double height, weight; | ||
Person(double a, double b) : height(a), weight(b) {} | ||
string to_string(){ | ||
return "weight: "+ std::to_string(weight) + ", height: "+ std::to_string(height); | ||
} | ||
}; | ||
|
||
|
||
namespace std { | ||
|
||
string to_string(list<int> l){ | ||
string s = ""; | ||
for(int a : l ){ | ||
s+=(" " + to_string(a) + " "); | ||
} | ||
return s; | ||
} | ||
|
||
} | ||
|
||
string to_string(std::vector<int> v){ | ||
string s = ""; | ||
for(int a : v ){ | ||
s += (" " + to_string(a) + " "); | ||
} | ||
return s; | ||
} | ||
|
||
|
||
void print(Stringable a){ | ||
std::cout << a.to_string() << std::endl; | ||
} | ||
|
||
void print(HasStringFunc a){ | ||
std::cout << to_string(a) << std::endl; | ||
} | ||
|
||
int main() { | ||
std::list<int> l {1, 2, 3}; | ||
Person p(57, 170.0); | ||
std::vector<int> v { 34, 23, 34, 56, 78}; | ||
|
||
print(p); // uses concept Stringable | ||
print(l); | ||
print(3); // uses concept HasStringFunc | ||
// print(v); // error | ||
return 0; | ||
} |
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,7 @@ | ||
all: $(patsubst %.cpp, %.out, $(wildcard *.cpp)) | ||
|
||
%.out: %.cpp Makefile | ||
clang++ $< -o $@ -std=c++2a -Xclang -fconcepts-ts -pedantic | ||
|
||
clean: | ||
rm *.out |