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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:12:11+00:00 2026-05-26T02:12:11+00:00

Say I have a function with template type T and two other classes A

  • 0

Say I have a function with template type T and two other classes A and B.

template <typename T>
void func(const T & t)
{
    ...........
    //check if T == A do something
    ...........
    //check if T == B do some other thing
}

How can I do these two checks (without using Boost library)?

  • 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-26T02:12:12+00:00Added an answer on May 26, 2026 at 2:12 am

    If you literally just want a boolean to test whether T == A, then you can use is_same, available in C++11 as std::is_same, or prior to that in TR1 as std::tr1::is_same:

    const bool T_is_A = std::is_same<T, A>::value;
    

    You can trivially write this little class yourself:

    template <typename, typename> struct is_same { static const bool value = false;};
    template <typename T> struct is_same<T,T> { static const bool value = true;};
    

    Often though you may find it more convenient to pack your branching code into separate classes or functions which you specialize for A and B, as that will give you a compile-time conditional. By contrast, checking if (T_is_A) can only be done at runtime.

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

Sidebar

Related Questions

Say I have this: template<typename T, int X> class foo { public: void set(const
Say I have a function func(i) that creates an object for an integer i,
Say I have a function foo: (defun foo (x y &rest args) ...) And
Lets say I have a function f[x_, y_] , and two lists l1 ,
Lets say you have simple template function (not class member for the sake of
Let's say that we have a function like so template <class T, class T2>
Let's say I have the following snippet: template <class T> void f(T arg) {
I have the following template template<typename T> void f(T t) { } And I
Say I have some sort of type wrapping up a function, maybe a lambda
Let's say I have two classes Foo and Bar, and I want to make

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.