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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:41:55+00:00 2026-05-23T10:41:55+00:00

I need to write template which generates some code depending on whether template parameter

  • 0

I need to write template which generates some code depending on whether template parameter is instance of of some class. The template can be generated for all classes but only in case the class is subclass of other class the code should be executed.

The problem is that function that should be implemented does not receive any instance of the class, so the only thing known is class name. So it is impossible to achieve this with dynamic_cast as it demands instance of the object

template<T>
class A 
{
  void somefunction(void)
  {
     if (T instanceof Foo) then ...
     else ...
  }
}

adding some explanation

class X: public Foo {};
class Y {};

class A<X> {} // special logic is generated
class A<Y> {} // special logic is NOT generated
  • 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-23T10:41:56+00:00Added an answer on May 23, 2026 at 10:41 am

    You can use template specialization or boost::is_base_of from the boost traits library
    Or of course write your own traits, but shouldn’t, for you have not mastered templates yet.

    Using specialization, you could

    template<T>
    class A 
    {
      void somefunction() {
         // generic version
      }
    };
    template<>
    class A <Foo>
    {
      void somefunction() {
         // foo version
      }
    };
    

    As always, let me recommend Vandevoorde/Josuttis “C++ Templates: The Complete Guide”.

    If you fear code bloat because only one memberfunction out of many needs to be specialized, you can still outsource that function:

    template <typename T> struct somefunction_helper {
        static void thatfunction () {
            // generic form
        }
    };
    template <> struct somefunction_helper<Foo> {
        static void thatfunction () {
            // Foo form
        }
    };
    
    template<T>
    class A 
    {
      void somefunction()  {
         somefunction_helper<T>::thatfunction();
      }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write a class in C++ in which one of the parameter
When I write class templates, and need to fully-specialize members of those classes, Doxygen
I need write an update statement that used multiple tables to determine which rows
I need to write a Java Comparator class that compares Strings, however with one
I need to write code that picks up PGP-encrypted files from an FTP location
I've got some VB code (actually VBA) which is basically the same except for
I need to write a page which allows a user to select price packages,
I need to write a templated function replace_all in C++ which will take a
I need to write some scripts for WinXP to support some of the analysts
I have various binary file formats which I need to dump to some kind

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.