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

Ask A Question

Stats

  • Questions 68k
  • Answers 68k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer What you're looking for is to have the ValueMember (=… May 11, 2026 at 12:11 pm
  • added an answer You need to add an alias for your namespace and… May 11, 2026 at 12:11 pm
  • added an answer Your char probably is represented signed. Cast it to unsigned… May 11, 2026 at 12:11 pm

Related Questions

Although I'm specifically interested in web application information, I would also be somewhat curious
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 I played with it before, I'm finally starting to use Dvorak (Simplified) regularly.
Although I grasp the concept of Bitwise Operators, I can't say that I have
Although I'm doubtful, I'm curious as to whether it's possible to extract primitive-type template
Although I know how to build a DOM the long, arduous way using the
Although I deeply fell in love with the MVVM pattern there seem to be
Although I'm still poking around in the docs and online, I figured I'd ask
I have a HashMap (although I guess this question applies to other collections) of

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.