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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:49:49+00:00 2026-05-26T21:49:49+00:00

I’m trying to recover the type of an array based on the size of

  • 0

I’m trying to recover the type of an array based on the size of its elements. I think I cannot use templates for this. Is there any way to achieve this without switch statements? Or if it is not possible without switch statements, is there a way to embed the switch statement into a class (caster). I like the caster class idea because there are many functions apart from copy that needs similar casting.

copy_region(uint8_t *, int);
copy_region(uint16_t *, int);
copy_region(uint32_t *, int);

void copier(uint8_t *ptr_element, int sz_element) {
   copy_region( caster(sz_element, ptr_element), n);
}

The switch solution I have is like this:

void copier(uint8_t *ptr_element, int sz_element) {
   switch(sz_element){
     case 1: copy_region( uint8_t*(ptr_element), n); break;
     case 2: copy_region( uint16_t*(ptr_element), n); break;
     case 4: copy_region( uint16_t*(ptr_element), n); break;
   }
}

I also thought of defining the operations as functors so that I might have a single switch statement, but the parameters for different operations are very dissimilar.

Edit:

The type information is not lost. I’m trying to implement a system (library/framework) with templated buffers. I have a base buffer class based on uint8_t as element. This class exists because I need to insert the buffers into a list. Base buffer class implements operations on these buffers. User specifies the buffer type as a template argument to the buffer class, but the library only sees the base buffer class and the type information (element size is enough to infer type).

  • 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-26T21:49:50+00:00Added an answer on May 26, 2026 at 9:49 pm
    template<int sz_element> void copier(uint8_t *ptr_element); //no definition
    template<1> void copier(uint8_t *ptr_element)
    {copy_region(uint8_t*(ptr_element), n);}
    template<2> void copier(uint8_t *ptr_element)
    {copy_region(uint16_t*(ptr_element), n);}
    template<4> void copier(uint8_t *ptr_element)
    {copy_region(uint32_t*(ptr_element), n);}
    template<8> void copier(uint8_t *ptr_element)
    {copy_region(uint64_t*(ptr_element), n);}
    

    If the sz_element is not known at compile time, then you have to use a switch like you had in the OP.

    There is no good that can come of this though. Why do you have a uint8_t* that points to arbitrary data? Get rid of that.

    [EDIT]
    You say you have a underlying class that is a buffer of uint8_t, and the users will use inherited classes with the type info. In that case, to have sane code, you need virtual functions:

    class base {
       vector<uint8_t> buffer;
    public:
       virtual void copy()=0;
       virtual ~base() {}
    };
    template <class type>
    class derived : public base {
    public:
       virtual void copy() {}
       ~derived() {}
    };
    

    This will allow your library to use the functions without knowing the types, and yet no type information is lost.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group
I know there's a lot of other questions out there that deal with this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text

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.