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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:53:32+00:00 2026-06-10T14:53:32+00:00

I have memory allocator which allocates memory for an object and calls its constructor

  • 0

I have memory allocator which allocates memory for an object and calls its constructor with any given arguments, see below.

    // 0 args to constructor
    template <class T>
    inline T* AllocateObject() { return new (InternalAllocate(sizeof(T), alignof(T))) T(); }

    // 1 args to constructor
    template <class T, typename arg0>
    inline T* AllocateObject(const arg0& a0) { return new (InternalAllocate(sizeof(T), alignof(T))) T(a0); }

    template <class T, typename arg0>
    inline T* AllocateObject(arg0& a0) { return new (InternalAllocate(sizeof(T), alignof(T))) T(a0); }

    // 2 args to constructor
    template <class T, typename arg0, typename arg1>
    inline T* AllocateObject(arg0& a0, arg1& a1) { return new (InternalAllocate(sizeof(T), alignof(T))) T(a0,a1); }

    template <class T, typename arg0, typename arg1>
    inline T* AllocateObject(const arg0& a0, arg1& a1) { return new (InternalAllocate(sizeof(T), alignof(T))) T(a0,a1); }

    template <class T, typename arg0, typename arg1>
    inline T* AllocateObject(arg0& a0, const arg1& a1) { return new (InternalAllocate(sizeof(T), alignof(T))) T(a0,a1); }

    template <class T, typename arg0, typename arg1>
    inline T* AllocateObject(const arg0& a0, const arg1& a1) { return new (InternalAllocate(sizeof(T), alignof(T))) T(a0,a1); }

    //.........

As you can see the number of calls grows with quite rapidly with the number of arguments. I have to alternate with ‘const’ and ‘non-const’ for each argument to make sure it plays fine with any argument I pass. (specifically, to be able to pass by references aswell as pass by value)

Is there any better way to perform the same task than to repeat this scheme? Basically I am looking at something like 8-10 arguments max and its just not very feasible I feel.

Thanks

  • 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-06-10T14:53:33+00:00Added an answer on June 10, 2026 at 2:53 pm

    Not a template solution, but a variable arguments #define could help you out of this problem.
    The exact format depends on your compiler, but in MSVC it would look like this:

    #define ALLOCATE_OBJECT(TYPE, ...) \
        ( new( InternalAllocate(sizeof(TYPE), alignof(TYPE)) ) TYPE(__VA_ARGS__) )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a method which constructs an object, calls an Execute method, and frees
I have a class which allocates memory on the heap and then the destructor
I have a C++ program which, during execution, will allocate about 3-8Gb of memory
I see that some C libraries have ability to specify custom memory allocators (
I have a Linux app (written in C) that allocates large amount of memory
I have an class A which uses a heap memory allocation for one of
On an embedded type system, I have created a Small Object Allocator that piggy
I want to create an allocator which provides memory with the following attributes: cannot
Given: Executable uses dll. They have different c/c++ runtime. Which restrictions do exist in
I have an windowless IViewObject object. I want to call its Draw function to

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.