惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

宝玉的分享
宝玉的分享
Apple Machine Learning Research
Apple Machine Learning Research
J
Java Code Geeks
V2EX - 技术
V2EX - 技术
Hacker News: Ask HN
Hacker News: Ask HN
T
Tailwind CSS Blog
V
Visual Studio Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
月光博客
月光博客
H
Hacker News: Front Page
D
DataBreaches.Net
GbyAI
GbyAI
Recorded Future
Recorded Future
IT之家
IT之家
H
Heimdal Security Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Schneier on Security
Schneier on Security
P
Privacy International News Feed
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
S
Security Affairs
博客园 - 三生石上(FineUI控件)
M
MIT News - Artificial intelligence
Google Online Security Blog
Google Online Security Blog
L
LINUX DO - 最新话题
Google DeepMind News
Google DeepMind News
The Cloudflare Blog
L
LangChain Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
腾讯CDC
The Last Watchdog
The Last Watchdog
I
Intezer
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Hacker News - Newest:
Hacker News - Newest: "LLM"
Stack Overflow Blog
Stack Overflow Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
U
Unit 42
H
Help Net Security
Simon Willison's Weblog
Simon Willison's Weblog
Y
Y Combinator Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
S
Schneier on Security
T
Tenable Blog
TaoSecurity Blog
TaoSecurity Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
小众软件
小众软件
B
Blog
S
Security @ Cisco Blogs
A
About on SuperTechFans
V
V2EX
T
The Exploit Database - CXSecurity.com

核桃的炼金工坊

2023 韩国游记 C++23: Flat Containers Deducing This 推し、燃ゆ Customization Point Object 2020 总结 搞了个 C++ 构建系统 软件设计哲学(NOTE) Paxos Note 关于 cpp 可见性的黑魔法后门 一个关于 private member function detect 的 SFINAE 模板 User-defined conversion and Copy elision VIM and Latex Compare Between CRTP and Virtual Interface in C++ Compile Time Reflection in C++11 C++11内存模型 在C++17中的部分新特性 Const Reference of Pointer
Stateful Metaprogramming
Hawtian Wang · 2023-08-01 · via 核桃的炼金工坊

A C++ trick is known as “stateful metaprogramming”, in which one manipulates friend functions and template instantiation to introduce a utilizable state into the compilation.

constexpr bool func() { /* something */ }

constexpr bool a = func();
constexpr bool b = func();
// Think this assertion always fails? With stateful metaprogramming, think again.
static_assert(a != b);

Maybe ill-formed? or UB?

auto flag(auto);

template<bool val>
struct flag_setter {
  friend auto flag(auto) {}
};

int main() {
  flag(1); // 1, error
  flag_setter<true>{};
  flag(1); // 2, ok
}

During the template class flag_setter instantiation, a template class specialization has been added to the global scope. The specialization defines the function flag() (by friend function). So the second invocation success.

auto flag(auto);

template<bool val>
struct flag_setter {
  friend auto flag(auto) {
  }

  bool value = false;
};

template<auto arg = 0, auto condition = requires { flag(arg); }>
consteval auto value() {
  if constexpr (!condition) {
    return flag_setter<condition> {}.value;
  }
  return condition;
}

int main() {
  constexpr auto a = value(); // 1: false
  constexpr auto b = value(); // 2: true
  static_assert(a != b);
}
  • At // 1 the template class flag_setter has not been instantiated. We got false.
  • After the first invocation, the template class flag_setter has been instantiated. The function flag() is defined. We got true.
template<std::size_t N>
struct reader
{
    friend auto counted_flag(reader<N>);
};

template<std::size_t N>
struct setter
{
    friend auto counted_flag(reader<N>) {}
    std::size_t value = N;
};

template<auto N = 0, auto tag = []{}, bool condition = requires(reader<N> red){ counted_flag(red); }>
constexpr auto next()
{
    if constexpr (!condition)
    {
        constexpr setter<N> s;
        return s.value;
    }
    else
    {
        return next<N + 1>();
    }
}

int main()
{
    constexpr auto a = next();
    constexpr auto b = next();
    constexpr auto c = next();
    static_assert(a == 0 && b == 1 && c == 2);
}

  1. Is stateful metaprogramming ill-formed (yet)?
  2. Revisiting Stateful Metaprogramming in C++20
  3. Compiler Explorer