I'm trying to implement a recursive structure in C++, something that should look kinda like this:
typedef struct { static constexpr int foo() { return 1; } typedef struct { // not valid - I meant foo() from "type" not from "recursive_type" static constexpr int foo() { return 2 * foo(); } // ? (there should be another recursive type here) } recursive_type; } type; and that should work like this:
static_assert(type::foo() == 1, "Nope"); static_assert(type::recursive_type::foo() == 2, "Nope"); static_assert(type::recursive_type::recursive_type::foo() == 4, "Nope"); Basically - I want recursive_type to contain structure that looks exactly like type, but its foo() returns twice the value of type's foo(). But as I have pointed in the comments, there are a couple of problems with my approach and sadly it doesn't work.
Can such a structure be declared somehow in C++, or maybe it is not possible?
struct(in the same translation unit) with the same name.recursive_typeshould spawn slightly modified version of the object it is called on)