Skip to content

Commit 482a1c9

Browse files
authored
code: fix concepts example to valid C++20 syntax (#320)
10.2.concepts.cpp used the Concepts TS syntax that was removed before C++20 was finalized, so it failed to compile under the project's own clang++ -std=c++2a build: - 'concept bool Name' -> 'concept Name' - return-type-requirement '{expr} -> string' -> '-> std::convertible_to<std::string>' - 'print(Stringable a)' -> 'print(Stringable auto a)' Add <concepts> for std::convertible_to. Verified it now compiles and runs with clang++ -std=c++2a -pedantic. Fixes #316
1 parent 4d3029b commit 482a1c9

1 file changed

Lines changed: 7 additions & 6 deletions

File tree

‎code/10/10.2.concepts.cpp‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,18 @@
1212
#include <string>
1313
#include <vector>
1414
#include <list>
15+
#include <concepts>
1516

1617
using namespace std;
1718

1819
template<typename T>
19-
concept bool Stringable = requires(T a){
20-
{a.to_string()} -> string;
20+
concept Stringable = requires(T a){
21+
{a.to_string()} -> std::convertible_to<std::string>;
2122
};
2223

2324
template<typename T>
24-
concept bool HasStringFunc = requires(T a){
25-
{ to_string(a) } -> string;
25+
concept HasStringFunc = requires(T a){
26+
{ to_string(a) } -> std::convertible_to<std::string>;
2627
};
2728

2829
struct Person {
@@ -55,11 +56,11 @@ string to_string(std::vector<int> v){
5556
}
5657

5758

58-
void print(Stringable a){
59+
void print(Stringable auto a){
5960
std::cout << a.to_string() << std::endl;
6061
}
6162

62-
void print(HasStringFunc a){
63+
void print(HasStringFunc auto a){
6364
std::cout << to_string(a) << std::endl;
6465
}
6566

0 commit comments

Comments
 (0)