作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有幸遇到的最心爱/邪恶的发明之一是 constexpr counter ,又名状态元编程。正如帖子中提到的,它在 C++14 下似乎是合法的,我想知道 C++17 有什么变化吗?
以下是主要基于帖子的实现
template <int N>
struct flag
{
friend constexpr int adl_flag(flag<N>);
constexpr operator int() { return N; }
};
template <int N>
struct write
{
friend constexpr int adl_flag(flag<N>) { return N; }
static constexpr int value = N;
};
template <int N, int = adl_flag(flag<N>{})>
constexpr int read(int, flag<N>, int R = read(0, flag<N + 1>{}))
{
return R;
}
template <int N>
constexpr int read(float, flag<N>)
{
return N;
}
template <int N = 0>
constexpr int counter(int R = write<read(0, flag<0>{}) + N>::value)
{
return R;
}
而我们use it作为
static_assert(counter() != counter(), "Your compiler is mad at you");
template<int = counter()>
struct S {};
static_assert(!std::is_same_v<S<>, S<>>, "This is ridiculous");
顺便说一句,这与Storing States in C++ Metaprogramming?直接矛盾
最佳答案
Defining a friend function in a template, then referencing that function later provides a means of capturing and retrieving metaprogramming state. This technique is arcane and should be made ill-formed.
Notes from the May, 2015 meeting:
CWG agreed that such techniques should be ill-formed, although the mechanism for prohibiting them is as yet undetermined.
这仍然是一个活跃的问题,至少目前 C++17 不会有任何改变。虽然当确定了这样的禁止机制时,这可能会被追溯为DR。
关于c++ - 有状态的元编程是病态的(还)吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52557830/
我是一名优秀的程序员,十分优秀!