Pas*_* By 40 c++ metaprogramming language-lawyer c++17
我有幸遇到的最让我最喜爱/最邪恶的发明之一是constexpr计数器,也就是有状态的元编程.正如帖子中所提到的,它似乎在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;
}
Run Code Online (Sandbox Code Playgroud)
我们用它作为
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");
Run Code Online (Sandbox Code Playgroud)
顺便说一下,这与C++ Metaprogramming中的存储状态直接矛盾吗?
Bar*_*rry 37
这是CWG活动问题2118:
在模板中定义友元函数,然后引用该函数提供了捕获和检索元编程状态的方法.这种技术是神秘的,应该是不正确的.
2015年5月会议记录:
CWG同意这些技术应该是不正确的,尽管禁止它们的机制尚未确定.
它仍然是一个活跃的问题,至少目前在C++ 17中没有任何改变.虽然确定了这种禁止机制,但这可能会被追溯性地裁定为DR.
| 归档时间: |
|
| 查看次数: |
2308 次 |
| 最近记录: |