选自modernescpp
作者:JP Tech等
机器之心编译
参与:Panda、杜伟
C++20(C++ 编程语言标准 2020 版)将是 C++ 语言一次非常重大的更新,将为这门语言引入大量新特性。 C++ 开发者 Rainer Grimm 通过一系列博客文章介绍 C++20 的新特性。 目前这个系列文章已经更新了两篇,本篇是第二篇,主要介绍了 C++20 的核心语言(包括一些新的运算符和指示符)。
#include <compare>
struct MyInt {
int value;
MyInt(int value): value{value} { }
auto operator<=>(const MyInt&) const = default;
};
struct Basics {
int i;
char c;
float f;
double d;
auto operator<=>(const Basics&) const = default;
};
struct Arrays {
int ai[1];
char ac[2];
float af[3];
double ad[2][2];
auto operator<=>(const Arrays&) const = default;
};
struct Bases : Basics, Arrays {
auto operator<=>(const Bases&) const = default;
};
int main() {
constexpr Bases a = { { 0, 'c', 1.f, 1. },
{ { 1 }, { 'a', 'b' }, { 1.f, 2.f, 3.f }, { { 1., 2. }, { 3., 4. } } } };
constexpr Bases b = { { 0, 'c', 1.f, 1. },
{ { 1 }, { 'a', 'b' }, { 1.f, 2.f, 3.f }, { { 1., 2. }, { 3., 4. } } } };
static_assert(a == b);
static_assert(!(a != b));
static_assert(!(a < b));
static_assert(a <= b);
static_assert(!(a > b));
static_assert(a >= b);
}
template<std::basic_fixed_string T>
class Foo {
static constexpr char const* Name = T;
public:
void hello() const;
};
int main() {
Foo<"Hello!"> foo;
foo.hello();
}
// aggregateInitialisation.cpp
#include <iostream>
struct Point2D{
int x;
int y;
};
class Point3D{
public:
int x;
int y;
int z;
};
int main(){
std::cout << std::endl;
Point2D point2D {1, 2};
Point3D point3D {1, 2, 3};
std::cout << "point2D: " << point2D.x << " " << point2D.y << std::endl;
std::cout << "point3D: " << point3D.x << " " << point3D.y << " " << point3D.z << std::endl;
std::cout << std::endl;
}
// designatedInitializer.cpp
#include <iostream>
struct Point2D{
int x;
int y;
};
class Point3D{
public:
int x;
int y;
int z;
};
int main(){
std::cout << std::endl;
Point2D point2D {.x = 1, .y = 2};
// Point2D point2d {.y = 2, .x = 1}; // (1) error
Point3D point3D {.x = 1, .y = 2, .z = 2};
// Point3D point3D {.x = 1, .z = 2} // (2) {1, 0, 2}
std::cout << "point2D: " << point2D.x << " " << point2D.y << std::endl;
std::cout << "point3D: " << point3D.x << " " << point3D.y << " " << point3D.z << std::endl;
std::cout << std::endl;
}
允许 [=, this] 作为 lambda capture,并通过 [=] 弃用这个的隐式 capture
struct Lambda {
auto foo() {
return [=] { std::cout << s << std::endl; };
}
std::string s;
};
struct LambdaCpp20 {
auto foo() {
return [=, this] { std::cout << s << std::endl; };
}
std::string s;
};
模板 lambda
template <typename T>
T operator(T x) const {
return x;
}
auto foo = []<typename T>(std::vector<T> const& vec) {
// do vector specific stuff
};
for(size_t i=0; i < v.size(); ++i){
if (unlikely(v[i] < 0)) sum -= sqrt(-v[i]);
else sum += sqrt(v[i]);
}
consteval int sqr(int n) {
return n*n;
}
constexpr int r = sqr(100); // OK
int x = 100;
int r2 = sqr(x); // Error
#include <iostream>
#include <string_view>
#include <source_location>
void log(std::string_view message,
const std::source_location& location = std::source_location::current())
{
std::cout << "info:"
<< location.file_name() << ":"
<< location.line() << " "
<< message << '\n';
}
int main()
{
log("Hello world!"); // info:main.cpp:15 Hello world!
}