using for Enumeration Values
??對(duì)比一下C++20前后的區(qū)別:
enum class State {
open,
progress,
done = 9
};
// Before C++20
void print(State s) {
switch (s) {
case State::open:
std::cout << "open\n";
break;
case State::done:
std::cout << "done\n";
break;
case State::progress:
std::cout << "progress\n";
break;
default:
assert(nullptr);
}
}
// Since C++20
void print1(State s) {
using enum State;
switch (s) {
case open:
std::cout << "open\n";
break;
case done:
std::cout << "done\n";
break;
case progress:
std::cout << "progress\n";
break;
default:
assert(nullptr);
}
}
void print2(State s) {
using State::open, State::done, State::progress;
switch (s) {
case open:
std::cout << "open\n";
break;
case done:
std::cout << "done\n";
break;
case progress:
std::cout << "progress\n";
break;
default:
assert(nullptr);
}
}
?
Range-Based for Loop with Initialization
??基于范圍的for循環(huán)是C++11引入的新特性,自C++20起,基于范圍循環(huán)也可以進(jìn)行初始化。
int main() {
std::vector v{1, 2, 3};
for (int i{1}; const auto& item : v)
std::cout << std::format("{}: {}\n", i++, item);
}
?
Feature Test Macros
??特性測(cè)試宏,正如其名,是為了當(dāng)前版本編譯器是否支持某個(gè)語(yǔ)言特性。這個(gè)宏以__cpp為前綴。
#if __cpp_generic_lambdas >= 201707
// generic lambdas with template parameters can be used
#endif
#ifndef __cpp_lib_as_const
template <typename T>
const T& as_const(T& t) {
return t;
}
#endif
?
Attribute [[no_unique_address]]
#include <iostream>
struct Empty {};
struct I {
int i;
};
struct S {
Empty e;
int i;
};
int main() {
std::cout << "sizeof(Empty): " << sizeof(Empty) << '\n';
std::cout << "sizeof(I): " << sizeof(I) << '\n';
std::cout << "sizeof(S): " << sizeof(S) << '\n';
}
??空類為了區(qū)分不同對(duì)象的地址,字節(jié)大小是1;而結(jié)構(gòu)體S由于內(nèi)存對(duì)齊的原因,所以字節(jié)大小是8。輸出結(jié)果毫無(wú)疑問(wèn)是1,4,8。
#include <iostream>
struct Empty {};
// EBCO
struct S : Empty {
int i;
};
// no_unique_address
struct S2 {
[[no_unique_address]] Empty e;
int i;
};
int main() {
std::cout << "sizeof(Empty): " << sizeof(Empty) << '\n';
std::cout << "sizeof(S): " << sizeof(S) << '\n';
std::cout << "sizeof(S2): " << sizeof(S2) << '\n';
}
??注解標(biāo)簽no_unique_address能起到和空基類優(yōu)化相同的效果。
?
Attributes [[likely]] and [[unlikely]]
??在if/else,switch分支當(dāng)中都可以使用,幫助編譯器作分支預(yù)測(cè)的優(yōu)化。
int f(int n) {
if (n <= 0) [[unlikely]] {
return n;
} else {
return n * n;
}
}
int g(int n) {
if (n <= 0) {
return n;
} else [[likely]] {
return n * n;
}
}
int h(int n) {
switch (n) {
case 1:
//
break;
[[likely]] case 2:
//
break;
}
return 0;
}
?
Attribute [[nodiscard]] with Parameter
??nodiscard用于修飾函數(shù),當(dāng)被修飾的函數(shù)發(fā)生調(diào)用(僅僅調(diào)用而不作賦值或者強(qiáng)制轉(zhuǎn)換操作),編譯器會(huì)報(bào)警告信息。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-747593.html
[[nodiscard]]
int f(int n) {
return n;
}
int main() {
f(1); // Warning
int n = f(1); // OK
reinterpret_cast<int *>(f(1)); // OK
}
?
New Character Type char8_t
using namespace std::literals;
auto c = u8'c'; // char8_t
auto s = u8"Hello World"; // const char8_t *
auto str1 = u8"Hello World"s; // std::u8string
auto str2 = u8"Hello World"sv; // std::u8string_view
std::cout << u8'c' << '\n'; // OK in C++17, error in C++20
std::cout << u8"Hello World\n"; // OK in C++17, error in C++20
std::cout << u8"Hello World\n"s; // OK in C++17, error in C++20
std::cout << u8"Hello World\n"sv; // OK in C++17, error in C++20
std::cout << c << '\n'; // OK in C++17, error in C++20
std::cout << s << '\n'; // OK in C++ 17, error in C++20
std::cout << str1 << '\n'; // OK in C++17, error in C++20
std::cout << str2 << '\n'; // OK in C++17, error in C++20
??更多的介紹將在下次帶來(lái),感謝支持??文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-747593.html
到了這里,關(guān)于C++20語(yǔ)言核心特性的變化的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!