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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:47:31+00:00 2026-05-30T15:47:31+00:00

I have a template class, and would like to write a member method that’s

  • 0

I have a template class, and would like to write a member method that’s able to recognize what kind of type the template has been instantiated to.

I need to create a string identifier containing the following information on the type:

  • bit depth
  • signed or unsigned
  • floating point or int or char

The method should return a string composed in the following way:

string: (BIT_DEPTH)-(U|S)-(C|I|F)

BIT_DEPTH -> is the number of bits used to represent type

U | S -> describes if type is signed or unsigned

C | I | F -> describes if type is char int or floating point

I thought of a way to find to bit depth:

int bitDepth = sizeof(TemplateType) * 8;

is it ok?

But have no idea on how to find the other information I need, unless a switch-case statement like the following is ok (but don’t think so):

THE FOLLOWING IS PSEUDO CODE THAT YOU SHOULD HELP ME EXPRESS IN A CORRECT SYNTAX

switch(TemplateType){

    case signed: ...;
    case unsigned: ...;

    default: ...;

}

My questions are two:

  • is bit depth calculation correct?
  • is the switch-case statement a good idea? (if yes can you please correct the syntax)
  • 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-30T15:47:32+00:00Added an answer on May 30, 2026 at 3:47 pm

    The bit calculation is OK, but can be improved by using CHAR_BIT instead of 8, see this question.

    To get the other information, you can use <type_traits>, specifically:

    • std::is_signed / std::is_unsigned
    • std::is_integral / std::is_floating_point

    Note that floating point types are always signed, but std::is_signed will return false, because it tests if the type is a signed integer.

    Also note that char is just another integral type, so there’s no standard type trait to specifically test that, but you can use a simple std::is_same<T, char>.

    In code, this might look like the following:

    #include <iostream>
    #include <type_traits>
    #include <climits> // CHAR_BIT
    
    template<class T>
    void f(){
      std::cout << sizeof(T) * CHAR_BIT << "-";
      if(std::is_integral<T>::value){
         if(std::is_signed<T>::value)
           std::cout << "S";
         else
           std::cout << "U";
         std::cout << "-";
         if(std::is_same<T, char>::value)
           std::cout << "C";
         else
           std::cout << "I";
      }else if(std::is_floating_point<T>::value){
        std::cout << "S-F";
      }
      std::cout << "\n";
    }
    

    Live example on Ideone.

    Note that bool counts as unsigned integer, but that is easily fixed. Also note that the compiler will spew a bunch of warnings regarding “conditional expression is constant”, so that can be improved, but this should suffice as a demonstration.

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

Sidebar

Related Questions

I want to have a template class that looks something like what I have
I have a C++ template class that gets instantiated with 3 different type parameters.
I have a template statistics class that has range parameters. template <typename T> class
I have a template class that I serialize (call it C), for which I
I have a template class like below. template<int S> class A { private: char
I have a template class that is only valid for couple of template parameters:
I have a template class where I want to use objects of that class
I have a template class defined like so: template <class T> class Command {
I'm trying to write code that uses a member typedef of a template argument,
I would like to write an object generator for a templated RAII class --

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.