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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:09:19+00:00 2026-06-05T19:09:19+00:00

I have a function in my namespace, ns::foo , whose job is to dispatch

  • 0

I have a function in my namespace, ns::foo, whose job is to dispatch an invocation of foo using argument-dependent lookup:

namespace ns
{

template<typename T>
void foo(T x)
{
  // call foo through ADL
  foo(x);
}

}

I want clients to be able to call foo without having to manually instantiate it, i.e.:

bar x;
ns::foo(x);

Not

ns::foo<bar>(x);

The problem of course is that ns::foo is recursive if there is no better match for foo than ns::foo.

I don’t wish to give ns::foo a different name, so is there any way to remove it from the overload set inside itself?

  • 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-05T19:09:20+00:00Added an answer on June 5, 2026 at 7:09 pm

    If the foo to where you want to dispatch is not in the ns namespace, then this should work:

    namespace helper
    {
        template<typename T>
        void _foo(T x)
        {
            // call foo through ADL
            foo(x);
        }
    }
    
    namespace ns
    {
        template<typename T>
        void foo(T x)
        {
          ::helper::_foo(x);
        }
    }
    

    The trick is that the call to foo from _foo will not consider ns::foo, because it is not in an argument-dependent namespace. Unless the type of x happens to be in ns of course, but then you have a recursion by definition.

    UPDATE: You have to put this code just after the definition of namespace ns:

    namespace ns
    {
         //your useful stuff here
    }
    namespace helper { /* template _foo */ }
    namespace ns { /* template foo */ }
    

    There is no recursion because the helper::_foo function cannot call the template foo because it is still not defined.

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

Sidebar

Related Questions

I have template function compare defined as below. #include<iostream> using namespace std; template<typename T>
Say I have a template function in namespace A. I also have another namespace
I'm trying to expose the following c++ function to python using boost.python: template <typename
I have the following code snippet: void foo(double a) {} namespace bar_space { struct
If a function is declared as such: namespace foo { void bar(); } Most
Let's say I have a namespace like that: var myNamespace = { foo: function()
I have the following code: namespace { void Foo() { } } namespace Bar
I have a helper assembly which includes a function to identify object types: namespace
I have function as below: foo :: Int -> a -> [a] foo n
#include <vector> #include <functional> #include <algorithm> using namespace std; struct Foo { int i;

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.