Loading AI tools
来自维基百科,自由的百科全书
C++17 是C++標準委員會於2017年制定的標準(ISO/IEC 14882),接替C++14標準,後被C++20標準取代。
在C++標準委員會制定此次的三年期標準之前,C++17標準的發布日期是不確定的。在這段日期中,C++17草案(draft)也被稱為C++1z,正如C++11草案名C++0x或C++1x,C++14草案名C++1y。C++17標準於2017年3月完成國際標準草案(Draft International Standard DIS)階段,並於同年12月完成最終標準發布。17標準對C++語法進行了加強,並對標準模板庫進行了些許修改,如給<algorithm>庫中算法增加了執行策略execution以支持並行計算。
static_assert
無需提供出錯信息[1]模板類構造函數能自動推導模板參數的類型(Class template argument deduction, CTAD)。例如允許以pair(5.0, false)
取代pair<double,bool>(5.0, false)
template <typename T = float>
struct MyContainer {
T val;
MyContainer() : val{} {}
MyContainer(T val) : val{val} {}
// ...
};
MyContainer c1 {1}; // OK MyContainer<int>
MyContainer c2; // OK MyContainer<float>
任何指定變量或變量模板的初始化的聲明,其聲明的類型是類模板(可能是 cv 限定的)。如std::pair p(2, 4.5); // deduces to std::pair<int, double> p(2, 4.5); new表達式,如template<class T>struct A{ A(T, T);}; auto y = new A{1, 2}; // allocated type is A<int>
依據auto的推到規則,在可允許類型的非類型模板參數情況下,可從實參類型推導出模板參數:
template <auto... seq>
struct my_integer_sequence {
// Implementation here ...
};
// Explicitly pass type `int` as template argument.
auto seq = std::integer_sequence<int, 0, 1, 2>();
// Type is deduced to be `int`.
auto seq2 = my_integer_sequence<0, 1, 2>();
摺疊表達式在運算符上執行模板參數包的展開計算。
template <typename... Args>
bool logicalAnd(Args... args) {
// Binary folding.
return (true && ... && args);
}
bool b = true;
bool& b2 = b;
logicalAnd(b, b2, true); // == true
template <typename... Args>
auto sum(Args... args) {
// Unary folding.
return (... + args);
}
sum(1.0, 2.0f, 3); // == 6.0
以前auto x {3};
將被推導為std::initializer_list<int>
, 現在推導為int
。[13][14]
auto x1 {1, 2, 3}; // error: not a single element
auto x2 = {1, 2, 3}; // x2 is std::initializer_list<int>
auto x3 {3}; // x3 is int
auto x4 {3.0}; // x4 is double
auto identity = [](int n) constexpr { return n; };
static_assert(identity(123) == 123);
constexpr auto add = [](int x, int y) {
auto L = [=] { return x; };
auto R = [=] { return y; };
return [=] { return L() + R(); };
};
static_assert(add(1, 2)() == 3);
constexpr int addOne(int n) {
return [n] { return n + 1; }();
}
static_assert(addOne(1) == 2);
以前this指針在lambda函數只能按引用捕獲。現在 *this 捕獲對象的副本,而 this (C++11)繼續捕獲引用。
struct MyObj {
int value {123};
auto getValueCopy() {
return [*this] { return value; };
}
auto getValueRef() {
return [this] { return value; };
}
};
MyObj mo;
auto valueCopy = mo.getValueCopy();
auto valueRef = mo.getValueRef();
mo.value = 321;
valueCopy(); // 123
valueRef(); // 321
過去關鍵字inline可用於函數聲明,現在也可以用於變量聲明,表示函數或定義可定義(但內容必須完全相同)多次。這允許在頭文件中定義一個內聯變量。
struct S {
S() : id{count++} {}
~S() { count--; }
int id;
static inline int count{0}; // declare and initialize count to 0 within the class
};
namespace A {
namespace B {
namespace C {
int i;
}
}
}
//The code above can be written like this:
namespace A::B::C {
int i;
}
變量定義初始化時,允許形如 auto [ x, y, z ] = expr;
,其中expr的「元組類似的對象」包括std::tuple, std::pair, std::array
等聚合結構。
using Coordinate = std::pair<int, int>;
Coordinate origin() {
return Coordinate{0, 0};
}
const auto [ x, y ] = origin();
x; // == 0
y; // == 0
std::unordered_map<std::string, int> mapping {
{"a", 1},
{"b", 2},
{"c", 3}
};
// Destructure by reference.
for (const auto& [key, value] : mapping) {
// Do something with key and value
}
if和switch語句允許如下的變量聲明和初始化:
{
std::lock_guard<std::mutex> lk(mx);
if (v.empty()) v.push_back(val);
}
// vs.
if (std::lock_guard<std::mutex> lk(mx); v.empty()) {
v.push_back(val);
}
Foo gadget(args);
switch (auto s = gadget.status()) {
case OK: gadget.zip(); break;
case Bad: throw BadFoo(s.message());
}
// vs.
switch (Foo gadget(args); auto s = gadget.status()) {
case OK: gadget.zip(); break;
case Bad: throw BadFoo(s.message());
}
//适用场景1:简化模版偏特化的写法
template <typename T>
constexpr bool isIntegral() {
if constexpr (std::is_integral<T>::value) {
return true;
} else {
return false;
}
}
static_assert(isIntegral<int>() == true);
static_assert(isIntegral<char>() == true);
static_assert(isIntegral<double>() == false);
struct S {};
static_assert(isIntegral<S>() == false);
//适用场景2:编写变参模版函数
template<int N, int...Ns>
int Sum()
{
if constexpr(0 == sizeof...(Ns))
return N;
else
return N+Sum<Ns...> ();
}
//使用场景3:替代enable_if。编写模板函数时,经常要使用enable_if语句来进行静态类型检查,保证模板输入的类型满足某种要求
template <typename T>
bool IsOdd(T input){
if constexpr (std::is_integral<T>::value) //
return static_cast<bool>(input % 2);
}
char x = u8'x';
enum byte : unsigned char {};
byte b {0}; // OK
byte c {-1}; // ERROR
byte d = byte{1}; // OK
byte e = byte{256}; // ERROR
C++17 引入了3個特性(attribute): fallthrough, nodiscard, maybe_unused.
//fallthrough指示编译器直通一个switch语句:
switch (n) {
case 1:
// ...
[[fallthrough]]
case 2:
// ...
break;
}
//nodiscard引发一个警告,当具有该特性的函数或类的返回值被忽略:
[[nodiscard]] bool do_something() {
return is_success; // true for success, false for failure
}
do_something(); // warning: ignoring return value of 'bool do_something()',
// declared with attribute 'nodiscard'
// Only issues a warning when `error_info` is returned by value.
struct [[nodiscard]] error_info {
// ...
};
error_info do_something() {
error_info ei;
// ...
return ei;
}
do_something(); // warning: ignoring returned value of type 'error_info',
// declared with attribute 'nodiscard'
//maybe_unused告诉编译器变量或参数可能未被使用:
void my_callback(std::string msg, [[maybe_unused]] bool error) {
// Don't care if `msg` is an error message, just log it.
log(msg);
}
__has_include運算符用在#if 和 #elif表達式,以檢查是否一個頭文件或源文件可否包含。
//下例有两个相同作用的头文件,如果主文件不能包含, 就使用backup/experimental头文件:
#ifdef __has_include
# if __has_include(<optional>)
# include <optional>
# define have_optional 1
# elif __has_include(<experimental/optional>)
# include <experimental/optional>
# define have_optional 1
# define experimental_optional
# else
# define have_optional 0
# endif
#endif
//也可以用在不同位置、不同名字但具有相同地位的头文件的包含:
#ifdef __has_include
# if __has_include(<OpenGL/gl.h>)
# include <OpenGL/gl.h>
# include <OpenGL/glu.h>
# elif __has_include(<GL/gl.h>)
# include <GL/gl.h>
# include <GL/glu.h>
# else
# error No suitable OpenGL headers found.
# endif
#endif
std::variant模板類實現了類型安全的union
std::variant<int, double> v{ 12 };
std::get<int>(v); // == 12
std::get<0>(v); // == 12
v = 12.0;
std::get<double>(v); // == 12.0
std::get<1>(v); // == 12.0
std::optional類模板可能包含一個值或者可能不包含值:
std::optional<std::string> create(bool b) {
if (b) {
return "Godzilla";
} else {
return {};//或者 return std::nullopt;
}
}
create(false).has_value();//返回true或false
*create(false);//返回值;如果无值,则返回T()
create(false).value_or("empty"); // == "empty"
create(true).value(); // == "Godzilla"
// optional-returning factory functions are usable as conditions of while and if
if (auto str = create(true)) {
// ...
}
std::any模板類是類型安全的包含任何類型的值:
std::any x {5};
x.has_value() // == true
std::any_cast<int>(x) // == 5
std::any_cast<int&>(x) = 10;
std::any_cast<int>(x) // == 10
std::string_view類是不擁有字符串的情況可以讀取其值:[17]
// Regular strings.
std::string_view cppstr {"foo"};
// Wide strings.
std::wstring_view wcstr_v {L"baz"};
// Character arrays.
char array[3] = {'b', 'a', 'r'};
std::string_view array_v(array, std::size(array));
std::string str {" trim me"};
std::string_view v {str};
v.remove_prefix(std::min(v.find_first_not_of(" "), v.size()));
str; // == " trim me"
v; // == "trim me"
std::string_view不提供c_str()成員函數。
std::invoke用於調用「可調用對象」。
template <typename Callable>
class Proxy {
Callable c;
public:
Proxy(Callable c): c(c) {}
template <class... Args>
decltype(auto) operator()(Args&&... args) {
// ...
return std::invoke(c, std::forward<Args>(args)...);
}
};
auto add = [](int x, int y) {
return x + y;
};
Proxy<decltype(add)> p {add};
p(1, 2); // == 3
std::apply把std::tuple參數應用於可調用對象:
auto add = [](int x, int y) {
return x + y;
};
std::apply(add, std::make_tuple(1, 2)); // == 3
std::filesystem可操作文件、目錄、路徑:[18]
const auto bigFilePath {"bigFileToCopy"};
if (std::filesystem::exists(bigFilePath)) {
const auto bigFileSize {std::filesystem::file_size(bigFilePath)};
std::filesystem::path tmpPath {"/tmp"};
if (std::filesystem::space(tmpPath).available > bigFileSize) {
std::filesystem::create_directory(tmpPath.append("example"));
std::filesystem::copy_file(bigFilePath, tmpPath.append("newFile"));
}
}
std::byte類型既不是字符類型,也不是算術類型,實際上是枚舉類型:
std::byte a {0};
std::byte b {0xFF};
int i = std::to_integer<int>(b); // 0xFF
std::byte c = a & b;
int j = std::to_integer<int>(c); // 0
//Moving elements from one map to another:
std::map<int, string> src {{1, "one"}, {2, "two"}, {3, "buckle my shoe"}};
std::map<int, string> dst {{3, "three"}};
dst.insert(src.extract(src.find(1))); // Cheap remove and insert of { 1, "one" } from `src` to `dst`.
dst.insert(src.extract(2)); // Cheap remove and insert of { 2, "two" } from `src` to `dst`.
// dst == { { 1, "one" }, { 2, "two" }, { 3, "three" } };
//Inserting an entire set:
std::set<int> src {1, 3, 5};
std::set<int> dst {2, 4, 5};
dst.merge(src);
// src == { 5 }
// dst == { 1, 2, 3, 4, 5 }
//Inserting elements which outlive the container:
auto elementFactory() {
std::set<...> s;
s.emplace(...);
return s.extract(s.begin());
}
s2.insert(elementFactory());
//Changing the key of a map element:
std::map<int, string> m {{1, "one"}, {2, "two"}, {3, "three"}};
auto e = m.extract(2);
e.key() = 4;
m.insert(std::move(e));
// m == { { 1, "one" }, { 3, "three" }, { 4, "two" } }
許多STL算法,如copy, find 和 sort,支持並行執行政策:seq, par, par_unseq, unseq分別表示"sequentially", "parallel", "parallel unsequenced", "unsequenced".[20]
std::vector<int> longVector;
// Find element using parallel execution policy
auto result1 = std::find(std::execution::par, std::begin(longVector), std::end(longVector), 2);
// Sort elements using sequential execution policy
auto result2 = std::sort(std::execution::seq, std::begin(longVector), std::end(longVector));
Seamless Wikipedia browsing. On steroids.
Every time you click a link to Wikipedia, Wiktionary or Wikiquote in your browser's search results, it will show the modern Wikiwand interface.
Wikiwand extension is a five stars, simple, with minimum permission required to keep your browsing private, safe and transparent.