【发布时间】:2019-08-15 22:24:49
【问题描述】:
是否可以即时声明新类型(空结构体或没有实现的结构体)?
例如
constexpr auto make_new_type() -> ???;
using A = decltype(make_new_type());
using B = decltype(make_new_type());
using C = decltype(make_new_type());
static_assert(!std::is_same<A, B>::value, "");
static_assert(!std::is_same<B, C>::value, "");
static_assert(!std::is_same<A, C>::value, "");
“手动”解决方案是
template <class> struct Tag;
using A = Tag<struct TagA>;
using B = Tag<struct TagB>;
using C = Tag<struct TagC>;
甚至
struct A;
struct B;
struct C;
但是对于模板/元一些魔术make_new_type() 函数会很好。
现在stateful metaprogramming is ill-formed 可以实现类似的事情吗?
【问题讨论】:
-
为什么有人要这样做?什么是典型用例?
-
每个 lambda 都有一个独特的类型 :) 据我所知,它们是“只给我一个独特的类型”的成语——C++11 中唯一的一个。
-
相关:unconstexpr。 (从 GCC 8 起不再工作,并且那里的代码可能是格式错误的 NDR)
-
这个问题中的“On the fly”并不意味着在运行时。
标签: c++ templates metaprogramming stateful compile-time-constant