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 5956031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:11:53+00:00 2026-05-22T18:11:53+00:00

Is it possible to automatically determine if a class is an abstract base class

  • 0

Is it possible to automatically determine if a class is an abstract base class at compile time?

I have an object factory that, via other generic code, sometimes gets instantiated with an Abstract Base Class type. The code doesn’t compile because it calls new T() on the ABC. I end up having to specialize the object factory create code for each ABC to instead assert(0) in this case. If it were possible to automatically determine if a type is an ABC at compile-time this specialization could be automated.

A simplified example follows:

// this program code compiles w/ gcc 4.4
#include <iostream>
#include <typeinfo>

// How to automatically specialize this class at compile-time?
template<typename T>
struct isAbstractBaseClass
{
  enum { VALUE = 0 };
};

// Factory to create T, lives in a struct to allow default template parameters
template<typename T, int ABSTRACT = isAbstractBaseClass<T>::VALUE >
struct Create
{
  static T* create()
  {
    return new T();
  }
};

// specialize Create for abstract base classes
template<typename T>
struct Create<T, 1>
{
  static T* create()
  {
    std::cout << "Cannot create and Abstract Base Class!\n";
    std::cout << "Create failed on type_info::name() = " << typeid(T).name() << "\n";
    return 0;
  }
};

struct Foo
{
  Foo() { std::cout << "Foo created\n"; }
};

struct Baz
{
  virtual void bar() = 0; // make this an Abstract Base Class
};

// template specialize on Baz to mark it as an Abstract Base Class
// My Question: is it possible to automatically determine this at compile-time?
template<> class isAbstractBaseClass<Baz> { enum { VALUE = 1 }; };


int main()
{
  std::cout << "Attempting to create a Foo class.\n";
  delete Create<Foo>::create();

  std::cout << "Attempting to create a Baz class.\n";
  delete Create<Baz>::create();

  return 0;
}

the output:

> c++ abstract.cpp && ./a.out
Attempting to create a Foo class.
Foo created
Attempting to create a Baz class.
Cannot create and Abstract Base Class!
Create failed on type_info::name() = 3Baz

edit 1 @jwismar pointed me towards Boost’s is_abstract implementation. Honestly, looking at the code and trying to deduce what boost is doing is very painful. Can someone boil down what trick they’re using? (edit 2 actually, I was looking at the wrong bit of code, and I figured it down below in edit 2)

@raj Yes, there is a constraint that the class must have a default public constructor. Its not entirely generic, but it provides functionality for 99% of the types I care about. Adding a create() method is not an option because I don’t control some of the classes being wrapped (3rd party code).

@DennisZickefoose The code does compile — with template specializations to handle ABCs. Yes, the design could be improved to ensure the code that instantiates the create() method with an ABC doesn’t do so, but that code is also performing other duties which make sense on ABCs and non-ABCs alike. At this point it would be a major re-write and I’m looking for a more short term solution.

Both @raj and @DennisZickefoose make good points about the design of the example and the underlying code base, but I’m really only interested in the topic’s question of how to determine ABC-ness of a type at compile time. Preferably without Boost. My rationale for such a need is orthogonal to the question at hand.

edit 2 Since I can’t answer my own question with out 100 reputation, I’ll post my answer here:

I was able to understand the Boost is_abstract code enough to create a version of isAbstractBaseClass that works for my needs. It uses SFINAE to fallback to the check_sig(…) version in the case of an ABC type.

template<class T>
struct isAbstractBaseClass
{
  // Inspired by boost/type_traits/is_abstract.hpp
  // Deduction fails if T is void, function type, 
  // reference type (14.8.2/2)or an abstract class type 
  // according to review status issue #337
  template<class U>
  static char check_sig(U (*)[1]);
  template<class U>
  static short check_sig(...);
  //
  enum { VALUE = sizeof(isAbstractBaseClass<T>::template check_sig<T>(0)) - 1 };
};
  • 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-22T18:11:54+00:00Added an answer on May 22, 2026 at 6:11 pm

    The Boost Type Traits library has an is_abstract functor. You could either use that directly, or take a look at the implementation and see how they handled it.

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

Sidebar

Related Questions

Often I have some compile-time constant number that is also the upper limit of
Is it possible to automatically encrypt files via 'git push' before transferring to a
Is it possible when using TFS server to automatically have it ensure a specific
Is it possible to automatically transfer/ftp files to a server, every time I commit
Is it somehow possible to automatically have a link to GitHub issue number in
Based on a project is it possible to automatically generate a class diagram with
is it possible to automatically export mysql database in phpmyadmin
Is it possible to automatically create Word documents which include list fields from a
I am using Eclipse to develop a Blackberry Java application.Is possible to automatically increment
While it is possible to generate PowerPoint presentations automatically using Office Automation , this

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.