Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6345693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:52:26+00:00 2026-05-24T20:52:26+00:00

Why is it that in the code below template<typename T> struct Child : public

  • 0

Why is it that in the code below template<typename T> struct Child : public Parent requires a definition, whereas template<typename T> struct Orphan does not require one (but the presence of one does not hurt)?

#include <iostream>

struct Parent {};

// A definition is necessary.
template<typename T>
struct Child : public Parent
{};

template<>
struct Child<int> : public Parent
{
    Child() {
        std::cout << "Child<int>::Child: full specialization\n";
    }
};

// No definition is necessary (but the presence of one doesn't hurt).
template<typename T>
struct Orphan;

template<>
struct Orphan<int>
{
    Orphan() {
        std::cout << "Orphan<int>::Orphan: full specialization\n";
    }
};

int main()
{
    Orphan<int> orphan;
    Child<int> child;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T20:52:26+00:00Added an answer on May 24, 2026 at 8:52 pm
    template<typename T>
    struct Orphan;
    

    This is a perfectly normal forward declaration.

    I suppose the language simply forbids forward declarations containing declaration of the parent. Such a syntax doesn’t exist.

    So it doesn’t have much to do with templates. You can’t do the following either

    class Base {};
    
    class Derived: public Base;  //forward declaration of Derived
    

    And in your case, you could forward declare Child as usual

    template <class T>
    class Child;
    

    Now the only question is: when we are trying to create an instance of the class, do we still only have a forward declaration or do we have a complete type.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My code does not compile. Below is my code template <typename T> class TemplateClass
I've the below code, template< typename T > class T1 { public: T i;
Here is the code that works fine : template<typename... Args> struct count; template<> struct
Background: I have a WPF UserControl (MainControl - not shown in code below) that
I know that the below code is a partial specialization of a class: template
Please, consider the code below: template<typename T> bool function1(T some_var) { return true; }
I understand that the code below would result segmentation fault because at the cstr
I have this javascript code below that uses jquery, it is suppoed to be
I have the code below that hides and shows the navigational bar. It is
I have written a very simple WCF service, that worked fine (code below), then

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.