constexpr_func

C++

shaozkshaozk cpp/constexpr_func.cpp · 19 行 · 更新于 2026-10-19

#include <iostream>

class Point {
public:
  constexpr Point(int x, int y) : x_(x), y_(y) {}
  constexpr sum() { return x_ + y_; }
private:
  int x_;
  int y_;
};

int main() {
  constexpr int a = 1;
  static_assert(a == 1);

  constexpr Point pt(1, 2);
  static_assert(pt.sum() == 3);
  return 0;
}

评论