-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Expand file tree
/
Copy path12.2.delete.with.reason.cpp
More file actions
27 lines (23 loc) 路 723 Bytes
/
Copy path12.2.delete.with.reason.cpp
File metadata and controls
27 lines (23 loc) 路 723 Bytes
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
//
// 12.2.delete.with.reason.cpp
// chapter 12 cpp26
// modern c++ tutorial
//
// created by changkun at changkun.de
// https://github.com/changkun/modern-cpp-tutorial
//
#include <iostream>
// C++26 lets a deleted function carry a reason, which the compiler
// reports if the function is selected.
void use_span(int* p, int n);
void use_span(std::nullptr_t) = delete("pass a real buffer, not nullptr");
int main() {
int buf[3] = {1, 2, 3};
use_span(buf, 3);
// use_span(nullptr); // error: call to deleted function: pass a real buffer, not nullptr
std::cout << "ok" << std::endl;
}
void use_span(int* p, int n) {
for (int i = 0; i < n; ++i) std::cout << p[i] << ' ';
std::cout << std::endl;
}