2411C++,学习C++提示4

结构绑定

cpp 复制代码
  auto [first, ...ts] = std::tuple{1, 2 ,3};
  assert(1 == first);

浮点作为非类型模板参数

cpp 复制代码
template<double Value> constexpr auto value = Value;

int main() {
    std::cout << value<4.2>; // prints 4.2
}
cpp 复制代码
template<double... Vl1s, double... Vl2s>
[[nodiscard]] consteval auto calc(values<Vl1s...>, values<Vl2s...>) {
    constexpr auto mul = (Vl2s * ...);
    return ((Vl1s * mul) + ...);
}
cpp 复制代码
template <double... List1, double... List2>
[[nodiscard]] consteval auto calc(values<List1...>, values<List2...>) {
    return (0 + ... + (List1 * (1 * ... * List2)));
}
cpp 复制代码
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;
}
cpp 复制代码
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;
}
cpp 复制代码
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;
}
cpp 复制代码
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;
}
cpp 复制代码
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;
}

不可达

cpp 复制代码
#include <utility>

int main() {
    std::unreachable();
    return 42; // invokes undefined behavior
}

抑制ADL

cpp 复制代码
namespace adl {
    struct foo {};
    void bar(foo) {}
}

int main() {
    adl::foo foo;
    bar(foo);   // OK,    ADL
    (bar)(foo); // error: no ADL
}

交换字节

cpp 复制代码
#include <bit>

int main() {
   constexpr auto value = std::uint16_t(0xCAFE);
   std::cout << std::hex << value; // pritns cafe
   std::cout << std::hex << std::byteswap(value); // prints feca
}

行多态

cpp 复制代码
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(missing_a{.b = 42}); // error
  shrink(foo{.a = 4, .b = 2}); // prints 4
  shrink(bar{.a = 42}); // prints 42
}

多维数组

cpp 复制代码
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]));
}

基于策略的设计

cpp 复制代码
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());
相关推荐
bikong712 分钟前
桥接模式,打造灵活可扩展的日志系统C++
c++·桥接模式
艾莉丝努力练剑16 分钟前
【C++】类和对象(下):初始化列表、类型转换、Static、友元、内部类、匿名对象/有名对象、优化
linux·运维·c++·经验分享
疋瓞30 分钟前
C++_STL和数据结构《1》_STL、STL_迭代器、c++中的模版、STL_vecto、列表初始化、三个算法、链表
数据结构·c++·算法
三体世界41 分钟前
测试用例全解析:从入门到精通(1)
linux·c语言·c++·python·功能测试·测试用例·测试覆盖率
Bear on Toilet1 小时前
继承类模板:函数未在模板定义上下文中声明,只能通过实例化上下文中参数相关的查找找到
开发语言·javascript·c++·算法·继承
努力努力再努力wz2 小时前
【c++进阶系列】:map和set的模拟实现(附模拟实现的源码)
java·linux·运维·开发语言·c++
lingran__5 小时前
速通ACM省铜第三天 赋源码(Double Perspective和Trip Shopping和Hamiiid, Haaamid... Hamid?)
c++·算法
凤城老人5 小时前
C++使用拉玛努金公式计算π的值
开发语言·c++·算法
YaoYuan93237 小时前
C++ 类型推导(第一部分)
c++
夜猫逐梦8 小时前
【VC】 error MSB8041: 此项目需要 MFC 库
c++·mfc