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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T20:54:47+00:00 2026-05-20T20:54:47+00:00

Loads of C++ libraries, the standard included, allow you to adapt your objects for

  • 0

Loads of C++ libraries, the standard included, allow you to adapt your objects for use in the libraries. The choice is often between a member function or a free function in the same namespace.

I’d like to know mechanics and constructs the library code uses to dispatch a call which will call one of these “extension” functions, I know this decision has to take place during compile time and involves templates. The following runtime psuedocode is not possible/non-sense, the reasons are out of the scope of this question.

if Class A has member function with signature FunctionSignature
    choose &A.functionSignature(...)
else if NamespaceOfClassA has free function freeFunctionSignature
    choose freeFunctionSignature(...)
else
    throw "no valid extension function was provided"

The code above looks like runtime code :/. So, how does the library figure out the namespace a class is in, how does it detect the three conditions, what other pitfalls are there that need to be avoided.

The motivation for my question is for me to be able to find the dispatch blocks in libraries, and to be able to use the constructs in my own code. So, detailed answers will help.

!!TO WIN BOUNTY!!

Ok so according to the answer from Steve (and the comments) ADL and SFINAE are the key constructs for wiring up the dispatch at compile time. I’ve got my head arround ADL (primitively) and SFINAE (again rudementary). But I don’t know how they orchistrate together in the way I think they should.

I want to see a illustrative example of how these two constructs can be put together so that a library can choose at compile time whether to call a user supplied member function in an object, or a user supplied free function supplied in the same object’s namespace. This should only be done using the two constructs above, no runtime dispatch of any sort.

Lets say the object in question is called NS::Car, and this object needs to provide the behaviour of MoveForward(int units), as a member function ofc. If the behaviour is to be picked up from the object’s namespace it will probably look like MoveForward(const Car & car_, int units). Lets define the function that wants to dispatch mover(NS::direction d, const NS::vehicle & v_) , where direction is an enum, and v_ is a base class of NS::car.

  • 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-20T20:54:48+00:00Added an answer on May 20, 2026 at 8:54 pm

    Well, I can tell you how to detect the presence of member functions of a certain name (and signature) at compile time. A friend of mine describes it here:

    Detecting the Existence of Member Functions at Compile-Time

    However that won’t get you where you want to go, because it only works for the static type. Since you want to pass a “reference-to-vehicle”, there is no way to test if the the dynamic type (the type of the concrete object behind the reference) has such a member function.

    If you settle for the static type though, there is another way to do a very similar thing.
    It implements “if the user provides an overloaded free function, call it, otherwise try to call the member function”. And it goes like this:

    namespace your_ns {
    
    template <class T>
    void your_function(T const& t)
    {
        the_operation(t); // unqualified call to free function
    }
    
    // in the same namespace, you provide the "default"
    // for the_operation as a template, and have it call the member function:
    
    template <class T>
    void the_operation(T const& t)
    {
        t.the_operation();
    }
    
    } // namespace your_ns
    

    That way the user can provide it’s own overload of “the_operation”,
    in the same namespace as his class, so it’s found by ADL. Of course
    the user’s “the_operation” must be “more specialized” than your default
    implementation – otherwise the call would be ambiguous.
    In practice that’s not a problem though, since everything that restricts
    the type of the parameter more than it being a reference-to-const to anything will be
    “more specialized”.

    Example:

    namespace users_ns {
    
    class foo {};
    
    void the_operation(foo const& f)
    {
        std::cout << "foo\n";
    }
    
    template <class T>
    class bar {};
    
    template <class T>
    void the_operation(bar<T> const& b)
    {
        std::cout << "bar\n";
    }
    
    } // namespace users_ns
    

    EDIT: after reading Steve Jessop’s answer again, I realize that’s basically what he wrote, only with more words 🙂

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

Sidebar

Related Questions

Dashcode loads a bunch of library listed in an array within part.js (function() {
When my app loads, I pull down a JSON representation of 99 objects. Each
Using struts2 with jsp with standard struts tag libraries. I'm trying to dynamically hide
My Ruby (JRuby) app loads a couple of Java libraries (not maintained by my
I'm trying to automate the process of accessing libraries in the framework I use
Are there any libraries for in-browser javascript that provide the same flexibility/modularity/ease of use
I've got a bookmarklet which loads jQuery and some other js libraries. How do
I need to write class that loads shared libraries. The dlopen() / dlerror() sequence
In codeigniter we have the standard classes of an mvc framework, models,views, libraries,etc. I'd
I do have a simple cmake project (on linux) that loads some libraries from

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.