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

  • Home
  • SEARCH
  • 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 6023145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:57:42+00:00 2026-05-23T03:57:42+00:00

This code: template <class T> class Foo {}; typedef Foo<void*> Bar; template <class T>

  • 0

This code:

template <class T>
class Foo {};

typedef Foo<void*> Bar;

template <class T>
class Foo<T*> : public Bar {};

// use Foo<int*> somewhere.

Compiles and works fine in MSVC 9.0, but doesn’t compile in GCC 4.1.1 or GCC 4.3.4, with the error:

error: invalid use of undefined type 'class Bar'

Is this illegal C++ that MSVC accepts incorrectly, or a limitation of GCC?

Either way, how can I work around this get the desired behaviour: pointer specialisations of Foo that inherit from unspecialised Foo<void*>?

  • 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-23T03:57:43+00:00Added an answer on May 23, 2026 at 3:57 am

    You cannot do that, except by writing the specialization for all T*, except when T is void. Otherwise, you will derive the class from itself, which for obvious reasons can’t work.

    Instantiating the primary class template for arguments that have an explicit or partial specialization is not possible. If you try to, by provoking an instantiation before the explicit or partial specialization is visible (note that your code did not provoke such an instantiation), your program is ill-formed, with no diagnostic being required (which effectively renders the behavior undefined).

    To achieve the above work-around, you can use SFINAE

    template <class T>
    struct isnt_void { typedef void type; };
    
    template<> struct isnt_void<void> { };
    
    template <class T, class = void>
    class Foo {};
    
    template <class T>
    class Foo<T*, typename isnt_void<T>::type> : public Foo<void*> {};
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following code snippet works for me: class Foo { public: template <class T>
Given class Foo template <typename T> class Foo { public: ...other methods.. void bar()
I have non-template class with a templatized constructor. This code compiles for me. But
Suppose I have code like this: template<class T, T initial_t> class Bar { //
I have code that looks like this: template<class T> class list { public: class
Consider the following example code: class Foo { }; class Bar : public Foo
Alot templated code looks like this: template <typename T> class foo { enum {
I have the following code, which does some iterator arithmetic: template<class Iterator> void Foo(Iterator
This code produces an error in the GNU compiler: class A { public: int
The following code works fine, a simple template class with a definition and a

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.