How does a structure inherit itself?

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()


How can this program be compiled? How does the structure inherit itself ?! I don't know what's going on here. Is this new felt in C ++?


source to share

2 answers

How does the structure itself inherit?

Not. A structure cannot inherit itself.

A struct that is an instance of a struct template can inherit from another instance of the same template if the parent instance has different template arguments.


is not a structure; this is a template. at_impl<It, when<true>>

is a template instance and it is a structure.

Note that the parent structure must be complete when the derived structure is created. This is possible if the parent is a specialization. Your example does not provide any definition of specialization at_impl<It, when<true>>

or any description at_impl


This is similar to functions. A function can call itself recursively, but the arguments to the function must change. If the function calls itself with the same arguments, then the recursion can never complete. A function with no arguments cannot be recursive ††.

Now the template has arguments and is similar to a function with parameters. The type has no template arguments and is similar to a function with no parameters. Instantiating a template casts to a type and is similar to calling a function.

† This is a necessary condition for terminating recursion, but not sufficient.

†† If the function does not depend on the global state, but from an abstract point of view, we should consider the global state as an implicit argument in this context.




is the pattern struct

/ class

from which a struct

/ is generated class


Various template arguments at_impl

generate different struct

s. Inheritance from another struct

/ is allowed class




All Articles