【发布时间】:2017-08-24 05:32:09
【问题描述】:
template <bool condition>
struct when;
template <typename It, typename = void>
struct at_impl : at_impl<It, when<true>> { };
struct at_t {
template <typename Xs, typename N>
constexpr decltype(auto) operator()(Xs&& xs, N const& n) const;
};
constexpr at_t at{};
int main()
{
}
这个程序怎么编译?结构如何继承自己?! 我不知道这里发生了什么。这是 c++ 中的新功能吗?
【问题讨论】:
-
你把自己弄糊涂了,没能区分 templates 和 types。
-
@KerrekSB:是的,我对模板不太熟悉。你能解释一下吗?为什么
at_impl不需要定义就可以派生出来? -
at_impl模板永远不会在您的程序中实例化。所以永远不会有从自身派生的类型。 -
@JohnSmith:继承是 types 做的事情,而不是模板。您的代码中没有任何内容“继承自身”。当模板被实例化时,它会导致一种类型恰好继承自另一种类型。
标签: c++ c++11 templates inheritance c++14