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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T21:01:17+00:00 2026-05-10T21:01:17+00:00

Although I’m doubtful, I’m curious as to whether it’s possible to extract primitive-type template

  • 0

Although I’m doubtful, I’m curious as to whether it’s possible to extract primitive-type template parameters from an existing type, perhaps using RTTI.

For example:

typedef std::bitset<16> WordSet; 

Would it be possible to extract the number 16 in the above code without hard-coding it elsewhere? Compiler specific implementations are welcome, though I’m particularly interested in g++.

  • 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. 2026-05-10T21:01:17+00:00Added an answer on May 10, 2026 at 9:01 pm

    It’s not possible in general to pick arbitrary template parameters.

    However, the usual way you do it is this:

    template<int N> struct foo {     static const int value = N; }; 

    and for types

    template<typename T> struct foo {     typedef T type; }; 

    You can access it then as foo<39>::value or foo<int>::type.

    If you have a particular type, you can use partial template specialization:

    template<typename> struct steal_it;  template<std::size_t N> struct steal_it< std::bitset<N> > {     static const std::size_t value = N; }; 

    The same principle is possible for type parameters too, indeed. Now you can pass any bitset to it, like steal_it< std::bitset<16> >::value (note to use size_t, not int!). Because we have no variadic many template paramters yet, we have to limit ourself to a particular parameter count, and repeat the steal_it template specializations for count from 1 up to N. Another difficulty is to scan types that have mixed parameters (types and non-types parameters). This is probably nontrivial to solve.

    If you have not the type, but only an object of it, you can use a trick, to still get a value at compile time:

    template<typename T> char (& getN(T const &) )[steal_it<T>::value];    int main() {     std::bitset<16> b;     sizeof getN(b); // assuming you don't know the type, you can use the object } 

    The trick is to make the function template auto-deduce the type, and then return a reference to a character array. The function doesn’t need to be defined, the only thing needed is its type.

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

Sidebar

Related Questions

Although I'm specifically interested in web application information, I would also be somewhat curious
Although using SQL FOR XML EXPLICIT is cumbersome, I find myself using it often.
Although I am not new to Python, this is my first attempt at using
I'm not new in C++ although I'm new in Linux. I'm using CMake to
Although somewhat related to this question , I have what I think is a
Although I don't have an iPhone to test this out, my colleague told me
Although I do understand the serious implications of playing with this function (or at
Although my question might seem abstract I hope it's not. Suppose I develop an
Although I played with it before, I'm finally starting to use Dvorak (Simplified) regularly.
Although ASP.NET MVC seems to have all the hype these days, WebForms are still

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.