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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:56:08+00:00 2026-05-13T18:56:08+00:00

How does the C++ compiler decide which function/method to call if there are multiple

  • 0

How does the C++ compiler decide which function/method to call if there are multiple possibilities?
In my specific case I have the standard free function of the C++ Run time and I also have a templated free variant, like this:

// The definitions of the C++ Run Time Library (from memory.h)
extern malloc(size_t s);
extern void free(void *p);

// Our own memory management functions
extern void *OurMalloc(size_t s);
extern void OurFree(void *p);

// Own variants to overrule malloc and free (instead of using #define)
template<typename T>
void *malloc(T t)
{
return OurMalloc(t);
}

template<typename T>
void free(T *t)
{
OurFree(t);
}

I tested this using the following code:

void main(void)
{
void *p = malloc(10);
free(p);
}

If I compile and run this, it seems that the call to malloc is correctly replaced by the templated variant. So far, so good.

However, the call to free is not replaced by the templated variant, and the standard C++ function is still called.

What rules does the C++ compiler use to decide which variant to give priority?
Is this related to the Koenig-lookup rules?

Note: I tried this alternative because using #define does not solve the problem (see question How to use C macro's (#define) to alter calls but not prototypes).

  • 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-13T18:56:09+00:00Added an answer on May 13, 2026 at 6:56 pm

    Overload resolution is quite complicated in general.

    In your case, it is quite easy: a function template is not considered if there is an exact match. For free it is the case (the standard free takes a void*), for malloc it isn’t (the standard malloc takes a size_t, you are passing an int and size_t can’t be a typedef for int — size_t is unsigned). If you call free with a type other than void*, it should instantiate your template.

    Running:

    #include <iostream>
    
    void* ml(size_t s)
    {
        std::cout << "ml(size_t)\n";
    }
    
    void fr(void *p)
    {
        std::cout << "fr(void*)\n";
    }
    
    template<typename T>
    void* ml(T t)
    {
        std::cout << "ml<" << typeid(T).name() << ">(T)\n";
    }
    
    template<typename T>
    void fr(T *t)
    {
        std::cout << "fr<" << typeid(T).name() << ">(T*)\n";
    }
    
    int main()
    {
        void* p1 = ml((size_t)10);
        fr(p1);
        int* p2 = (int*)ml(10);
        fr(p2);
        return 0;
    }
    

    I get

    ml(size_t)
    fr(void*)
    ml<i>(T)
    fr<i>(T*)
    

    and i is what returns typeid(int).name()

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer In SPD, navigate to _catalogs/masterpage. Right click on the customWSPTest1.master… May 14, 2026 at 1:27 am
  • Editorial Team
    Editorial Team added an answer If you're trying to access the window objects of other… May 14, 2026 at 1:27 am
  • Editorial Team
    Editorial Team added an answer A class in Java can be garbage-collected when nothing references… May 14, 2026 at 1:27 am

Related Questions

Say I have three classes: class X{}; class Y{}; class Both : public X,
ISO C++ says that the inline definition of member function in C++ is the
I doubt if there is a way to make compile-time conditions in Java like
I have to choose a platform for our product. I have to decide between
The project I work on has recently been switched from a horribly antiquated revision

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.