切腹のイラスト

C++でhello, world

{
  date: "",
  update: "",
  category: "/memo",
  tags: ["C++", "helloworld"]
}

久々に書いたのでメモ。

Makefile

CXX = clang++
CXXFLAGS = -std=c++23 -fmodule-file=std=std.pcm

hoge: std.pcm

std.pcm: /usr/share/libc++/v1/std.cppm
	$(CXX) $(CXXFLAGS) --precompile $< -o $@

hoge.cc

import std;

using namespace std::literals;

constexpr const auto s{u8"hello, world"s};

constexpr const struct {
  auto const operator <=> (int const &) const & {
    return <::> <class=int> requires requires { requires not int(); } {
      std::println("{}", s | std::ranges::to<std::string>());
    } (),0'0;
  }
  %>_<% // ←かわいい
};

auto main(int const, char const*const*const, char const*const*const)
  -> int try {:: _<3 ;;} catch(...) {}

実行

% make && ./hoge
hello, world