结构绑定
auto [first, ...ts] = std::tuple{1, 2 ,3};
assert(1 == first);
浮点作为非类型模板参数
template<double Value> constexpr auto value = Value;
int main() {
std::cout << value<4.2>;
}
template<double... Vl1s, double... Vl2s>
[[nodiscard]] consteval auto calc(values<Vl1s...>, values<Vl2s...>) {
constexpr auto mul = (Vl2s * ...);
return ((Vl1s * mul) + ...);
}
template <double... List1, double... List2>
[[nodiscard]] consteval auto calc(values<List1...>, values<List2...>) {
return (0 + ... + (List1 * (1 * ... * List2)));
}
template<double... Values> struct values {};
template<double... Vl1s, double... Vl2s>
[[nodiscard]] consteval auto calc(values<Vl1s...>, values<Vl2s...>) {
return ((Vl1s * (Vl2s * ...)) + ...);
}
template<double Epsilon = 0.1>
[[nodiscard]] consteval auto eq(double a, double b) {
return std::fabs(a - b) < Epsilon;
}
template<double... Values> struct values {};
template<double... Vl1s, double... Vl2s>
[[nodiscard]] consteval auto calc(values<Vl1s...>, values<Vl2s...>) {
constexpr auto c = (Vl2s * ...);
return ((Vl1s * c) + ...);
}
template<double Epsilon = 0.1>
[[nodiscard]] consteval auto eq(double a, double b) {
return std::fabs(a - b) < Epsilon;
}
template<double... Values> struct values {};
template<double... Vl1s, double... Vl2s>
[[nodiscard]] consteval auto calc(values<Vl1s...>, values<Vl2s...>) {
return (Vl1s + ...) * (Vl2s * ...);
}
template<double Epsilon = 0.1>
[[nodiscard]] consteval auto eq(double a, double b) {
return std::fabs(a - b) < Epsilon;
}
template<double... Values> struct values{};
template<double Vl1, double... Vl2s>
[[nodiscard]] consteval auto mult(){
return (Vl1 * ... * Vl2s);
}
template<double... Vl1s, double... Vl2s>
[[nodiscard]] consteval auto calc(values<Vl1s...>, values<Vl2s...>){
return (... + mult<Vl1s, Vl2s...>());
}
template<double Epsilon = 0.1>
[[nodiscard]] consteval auto eq(double a, double b) {
return std::fabs(a - b) < Epsilon;
}
template<double... Values> struct values {};
template<double... Vl1s, double... Vl2s>
[[nodiscard]] consteval auto calc(values<Vl1s...>, values<Vl2s...>){
return (0.0 + ... + (Vl1s * (Vl2s * ... )));
}
template<double Epsilon = 0.1>
[[nodiscard]] consteval auto eq(double a, double b) {
return std::fabs(a - b) < Epsilon;
}
不可达
#include <utility>
int main() {
std::unreachable();
return 42;
}
抑制ADL
namespace adl {
struct foo {};
void bar(foo) {}
}
int main() {
adl::foo foo;
bar(foo);
(bar)(foo);
}
交换字节
#include <bit>
int main() {
constexpr auto value = std::uint16_t(0xCAFE);
std::cout << std::hex << value;
std::cout << std::hex << std::byteswap(value);
}
行多态
struct foo {
int a{};
int b{};
};
struct bar {
int a{};
};
struct missing_a {
int b{};
};
struct row_with_member_a {
constexpr explicit(false) row_with_member_a(const auto& t)
: a{t.a}
{ }
int a{};
};
auto shrink(row_with_member_a r) {
std::cout << r.a;
}
int main() {
shrink(foo{.a = 4, .b = 2});
shrink(bar{.a = 42});
}
多维数组
template <class T, auto Dimensions> class mdarray2 {
public:
template <class I1, class I2> constexpr T &operator[](I1 i1, I2 i2) {
return vs_[i1][i2];
}
private:
std::array<std::array<T, 2>, Dimensions> vs_{};
};
int main() {
mdarray2<int, 2> a{};
a[1, 1] = 42;
assert(0 == (a[0, 0]));
assert(42 == (a[1, 1]));
}
基于策略的设计
template<class TPolicy>
struct foo : TPolicy {
static constexpr auto bar() {
return TPolicy::bar();
}
};
template<auto N>
struct policy { static constexpr auto bar() { return N; } };
static_assert(0 == foo<policy<0>>::bar());
static_assert(42 == foo<policy<42>>::bar());