support bazel complie this project and format code.
This commit is contained in:
15
learn_class/modern_cpp_30/literalAssert/BUILD
Normal file
15
learn_class/modern_cpp_30/literalAssert/BUILD
Normal file
@@ -0,0 +1,15 @@
|
||||
# please run `bazel run //learn_class/modern_cpp_30/literalAssert:literal`
|
||||
# please run `bazel run //learn_class/modern_cpp_30/literalAssert:assert`
|
||||
|
||||
load("@rules_cc//cc:defs.bzl", "cc_binary")
|
||||
|
||||
cc_binary(
|
||||
name = "literal",
|
||||
srcs = ["literal.cpp"],
|
||||
copts = ["-std=c++17"],
|
||||
)
|
||||
cc_binary(
|
||||
name = "assert",
|
||||
srcs = ["assert.cpp"],
|
||||
copts = ["-std=c++11"],
|
||||
)
|
14
learn_class/modern_cpp_30/literalAssert/assert.cpp
Normal file
14
learn_class/modern_cpp_30/literalAssert/assert.cpp
Normal file
@@ -0,0 +1,14 @@
|
||||
//
|
||||
// Created by light on 19-12-25.
|
||||
//
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
|
||||
int main() {
|
||||
const int alignment = 5;
|
||||
assert((alignment & (alignment - 1)) == 0);
|
||||
static_assert((alignment & (alignment - 1)) == 0,
|
||||
"Alignment must be power of two");
|
||||
return 0;
|
||||
}
|
18
learn_class/modern_cpp_30/literalAssert/default_delete.cpp
Normal file
18
learn_class/modern_cpp_30/literalAssert/default_delete.cpp
Normal file
@@ -0,0 +1,18 @@
|
||||
//
|
||||
// Created by light on 19-12-25.
|
||||
//
|
||||
|
||||
class myFun {
|
||||
public:
|
||||
myFun() = default;
|
||||
|
||||
myFun(const myFun &) = default;
|
||||
|
||||
myFun &operator=(const myFun &) = default;
|
||||
|
||||
myFun(myFun &&) = delete;
|
||||
|
||||
myFun &operator=(myFun &&) = delete;
|
||||
|
||||
~myFun() = default;
|
||||
};
|
71
learn_class/modern_cpp_30/literalAssert/literal.cpp
Normal file
71
learn_class/modern_cpp_30/literalAssert/literal.cpp
Normal file
@@ -0,0 +1,71 @@
|
||||
//
|
||||
// Created by light on 19-12-25.
|
||||
//
|
||||
|
||||
#include <chrono>
|
||||
|
||||
#include <bitset>
|
||||
#include <complex>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
|
||||
using namespace std::literals::chrono_literals;
|
||||
using namespace std::literals::string_literals;
|
||||
using namespace std::literals::complex_literals;
|
||||
|
||||
struct length {
|
||||
double value;
|
||||
enum unit {
|
||||
metre,
|
||||
kilometre,
|
||||
millimetre,
|
||||
centimetre,
|
||||
inch,
|
||||
foot,
|
||||
yard,
|
||||
mile,
|
||||
};
|
||||
static constexpr double factors[] = {1.0, 1000.0, 1e-3, 1e-2,
|
||||
0.0254, 0.3048, 0.9144, 1609.344};
|
||||
|
||||
explicit length(double v, unit u = metre) { value = v * factors[u]; }
|
||||
};
|
||||
|
||||
length operator+(length lhs, length rhs) {
|
||||
return length(lhs.value + rhs.value);
|
||||
}
|
||||
|
||||
length operator"" _m(long double v) { return length(v, length::metre); }
|
||||
|
||||
length operator"" _cm(long double v) { return length(v, length::centimetre); }
|
||||
|
||||
// 可能有其他运算符
|
||||
int main() {
|
||||
|
||||
std::cout << "i * i = " << 1i * 1i << std::endl;
|
||||
std::cout << "Waiting for 500ms" << std::endl;
|
||||
std::this_thread::sleep_for(500ms);
|
||||
std::cout << "Hello world"s.substr(0, 5) << std::endl;
|
||||
|
||||
length l1 = length(1.0, length::metre);
|
||||
length l2 = length(1.0, length::centimetre);
|
||||
std::cout << l2.value << std::endl;
|
||||
std::cout << (l1 + l2).value << std::endl;
|
||||
|
||||
// 1.0_m + 10.0_cm
|
||||
std::cout << (1.0_m + 1.0_cm).value << std::endl;
|
||||
|
||||
// 二进制字面量
|
||||
unsigned mask = 0b1101;
|
||||
// 以十进制打印
|
||||
std::cout << mask << std::endl;
|
||||
|
||||
// 打印二进制字面量
|
||||
std::cout << std::bitset<4>(mask) << std::endl;
|
||||
|
||||
// 数字分隔符
|
||||
unsigned mk = 0b111'000'000;
|
||||
double pi = 3.141'5926;
|
||||
return 0;
|
||||
}
|
29
learn_class/modern_cpp_30/literalAssert/overridefinal.cpp
Normal file
29
learn_class/modern_cpp_30/literalAssert/overridefinal.cpp
Normal file
@@ -0,0 +1,29 @@
|
||||
//
|
||||
// Created by light on 19-12-25.
|
||||
//
|
||||
|
||||
class A {
|
||||
public:
|
||||
virtual void foo();
|
||||
virtual void bar();
|
||||
void foobar();
|
||||
};
|
||||
|
||||
class B : public A {
|
||||
public:
|
||||
void foo() override; // OK
|
||||
void bar() override final; // OK
|
||||
// void foobar() override;
|
||||
// 非虚函数不能 override
|
||||
};
|
||||
|
||||
class C final : public B {
|
||||
public:
|
||||
void foo() override; // OK
|
||||
// void bar() override;
|
||||
// final 函数不可 override
|
||||
};
|
||||
|
||||
class D : public C {
|
||||
// 错误:final 类不可派生
|
||||
};
|
Reference in New Issue
Block a user