0

Consider something like this in C#:

class C<T> {}
class D : C<E> {}
class E : C<D> {}

Is an equivalent construction possible in C++ using templates?

markonius
  • 625
  • 6
  • 25
  • My point isn't translating a language. I only used C# to present an idea. I used C# because that's how my thought process went and it's at least somewhat related. I could present the same idea with arbitrary pseudocode, e.g. `let a = { let b = b }; let b = { let a = a };`. Anyway, [here's the new question](https://stackoverflow.com/questions/67942528/circular-reference-of-templates-in-c-when-complete-types-are-required) – markonius Jun 11 '21 at 19:23

1 Answers1

5

Yes, you can forward declare E:

template <typename T> class C {};

class E;
class D : public C<E> {};
class E : public C<D> {};

or, as per Franks suggestion:

template <typename T> class C {};

class D : public C<class E> {};
class E : public C<D> {};

Whether this works in your real case depends on whether C requires T to be complete (it does not in this stripped down example).

PS: I don't know C#, so I am not sure if you wanted private or public inheritance. The above uses public inheritance, while the default for classes declared with the keyword class is private inheritance.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • It's also possible to inline the forward declaration within the template arguments: `class D : C {};` –  Jun 11 '21 at 18:33
  • You would also probably want public inheritance for classes in C++ (unless OP's C# example is also private inheritance? I'm not sure, I've never used C#). – mediocrevegetable1 Jun 11 '21 at 18:34
  • @Frank yeah but I don't like it too much. Matter of style. For me it looks too uncommon and I would be tempted to remove it because it looks like someone put it there just because without knowing why. – 463035818_is_not_an_ai Jun 11 '21 at 18:35
  • 2
    Agreed that it's a matter of style. I personally like it because it makes it explicit that the type is expected to not be complete. Unlike a regular template that may or may not be inheriting from a complete type depending on stuff that's headers away. –  Jun 11 '21 at 18:36
  • Thanks for clearing this up, I'll include a more complex example in the question in a minute, please consider it. – markonius Jun 11 '21 at 18:41
  • @markonius more complex C# will be not something I can understand. Anyhow It would be better if you could show what you already tried in C++ – 463035818_is_not_an_ai Jun 11 '21 at 18:42
  • I don't think any other language has "private inheritance". Inheriting the implementation, not the interface is a strange concept when you think about it. Not to mention protected inheritance. – Aykhan Hagverdili Jun 11 '21 at 18:42
  • @AyxanHaqverdili mkay, i made it public. – 463035818_is_not_an_ai Jun 11 '21 at 18:44
  • @463035818_is_not_a_number I'm sure there is nothing hard to understand in this example. You may consider it pseudocode. – markonius Jun 11 '21 at 18:53
  • 1
    @markonius in general I am a little pedantic on changing the question after answers have been written (not only because it is my answer), though admittetly I knew that there was more to come when I wrote this answer, otherwise I could have just closed as duplicate of this one: https://stackoverflow.com/questions/4757565/what-are-forward-declarations-in-c/4757718#4757718. I didnt do that because I knew that your question lacks some details. Nevermind, writing good questions is difficult (actually writing answers is much easier). The issue was just that I was away from keys for some time. – 463035818_is_not_an_ai Jun 11 '21 at 19:36
  • welp, sorry for pulling the rug from under you then :D [Here's the new question](https://stackoverflow.com/questions/67942528/circular-reference-of-templates-in-c-when-complete-types-are-required#67942606) – markonius Jun 11 '21 at 20:13
  • Aslo, please don't close as duplicate of that one, I don't think it's an obvious implication, for someone who doesn't already know how to do this. – markonius Jun 11 '21 at 20:16
  • 2
    @markonius thats no reason to not close it as duplicate. A question getting closed as duplicate is not the same as a question getting deleted. Often duplicates make good entry points for future readers, though the answer needs to be at only one place. This answer here really does not add much to the one you can already find elsewhere – 463035818_is_not_an_ai Jun 11 '21 at 20:19