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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:02:48+00:00 2026-05-18T04:02:48+00:00

We have a structure like below: template<size_t size> class Container{ public: char characters[size]; };

  • 0

We have a structure like below:

template<size_t size>
class Container{
 public:
  char characters[size];
};

And we have a function like below:

template <size_t size>
void function(Container<size> input,size_t Size){
 //all instances do exactly same thing and with regard to Size that determines the size of object
}

Now in C++ for every value of size, a different instance of function will be created and apparently that’s not right in this case since all instances do the same thing, to avoid this the first function parameter should be a pointer(char * pointer) instead of an object so that accepts any array with any size eliminating the need for function to be templated, but I’m curious having a single function which accepts a variable-size parameter like above that’s not allowed by C++ is something impossible at all to implement and generate assembly for, or somehow leads to an inefficient implementation in terms of speed/memory ?

  • 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-18T04:02:48+00:00Added an answer on May 18, 2026 at 4:02 am

    Generally, where you need an IN parameter, pass built-in types by value, and other types by reference to const, i.e.,

    template< size_t size >
    void function( Container<size> const& input,size_t Size )
    {
     //all instances do exactly same thing and with regard to Size that determines the size of object
    }
    

    With this definition, chances are that the compiler+linker will optimize things so that there will be only one machine code version of function.

    I was a bit surprised the first time I checked a simple small example program, that expressing it with compile time polymorphism (templating) produced smaller and more efficient code than expressing it with run time polymorphism!

    Try it yourself, and if you’re as surprised as I once was, then Good! Otherwise, chances are that there’ll be no significant difference. But in some corner case you may find what in the old days was called “template code bloat”, and then it’s time to ignore it or measure whether it’s significant enough to do work on translating to run time polymorphism.

    Now to your question,

    “I’m curious having a single function
    which accepts a variable-size
    parameter like above that’s not
    allowed by C++ is something impossible
    at all to implement and generate
    assembly for, or somehow leads to an
    inefficient implementation in terms of
    speed/memory?”

    No, it’s not impossible to transform the compile time polymorphism to efficient run time polymorphism, or to simply no polymorphism. Especially since you’re already passing a run-time size (which presumably is guaranteed smaller than the fixed size).

    A safe way is to use std::string from the C++ standard library, header <string>. That involves dynamic allocation somewhere in the internals of std::string, done automatically for you, but affecting efficiency. But your code, containing char[size] characters, was not valid C++, and that indicates beginner level, so chances are that your design was not chosen for any good reason — hence, do:

    class Container
    {
    public:
        std::string characters;
    };
    
    void function( Container const& input )
    {
        // whatever, using e.g. input.characters.length()
    }
    

    Cheers & hth.,

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

Sidebar

Related Questions

No related questions found

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.