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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:38:16+00:00 2026-06-17T13:38:16+00:00

If I have a default function ‘foo’ defined in a namespace in one header

  • 0

If I have a default function ‘foo’ defined in a namespace in one header file:

//DefaultFoo.h
namespace DefaultFooNamespace
{
    template <typename T>
    void foo(T& x){/*...*/}
}

and overloads of foo defined in another namespace in one or more other header files, e.g., FooOverloads.h:

//FooOverloads.h
namespace FooOverloadsNamespace
{
    template <typename T>
    void foo(std::vector<T>& x){/*...*/}

    void foo(std::string& x){/*...*/}

    //etc.
}

and I have another function ‘bar’ which calls foo after bringing both DefaultFooNamespace and FooOverloadsNamespace namespaces into scope (note that I #include only DefaultFoo.h in Bar.h, as DefaultFoo.h contains a single function in a namespace which is intended never to be extended, unlike FooOverloadsNamespace which will be extended by the addition of additional overloads):

//Bar.h
#include "DefaultFoo.h" 

namespace BarNamespace
{
    template <typename T>
    void bar(T& x)
    {
        //... do stuff then call foo
        using DefaultFooNamespace::foo;
        using FooOverloadsNamespace::foo;
        foo(x);
    }
}

‘bar’ won’t compile unless I either make sure FooOverloads.h is #include’d before a #include of Bar.h, or I make sure that FooOverloads.h is #include’d in Bar.h, or alternatively I provide a declaration of a ‘foo’ function for a dummy class in FooNamespace and #include that in Bar.h, e.g.

//Dummy.h:
struct DummyClass
{
private:
    DummyClass(){}
};
namespace FooNamespace
{
    inline void foo(DummyClass& x);  //purely to open up namespace
}

//Bar.h
#include "Dummy.h"

Is there any way around this, such that I can define bar and avoid having to create redundant code to open up FooNamespace in Bar?

  • 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-17T13:38:17+00:00Added an answer on June 17, 2026 at 1:38 pm

    My solution would be to get rid of the useless DefaultFooNamespace namespace. Put the default foo in the same namespace as the overloads, then bar just does:

    using FooNamespace::foo;
    foo(x);
    

    The default foo is used if the caller hasn’t included a definition of a more appropriate one for the type of x

    I don’t see a reason for the default foo to be in a separate namespace, especially because doing that requires a separate dummy definition to make bar valid code, when the same overload could serve both purposes.

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

Sidebar

Related Questions

I have used default mail() function to send email it sends emails to yahoo
i have the following function to override the default save function in a model
I have this code to get the default constructor: Public Function f(ByVal t As
Following the good jQuery Plugins/Authoring instructions I have a little question (function($){ // Default
I have a function that restores all of the default settings of my application.
I have some questions about the default values in a function parameter list Is
Is there a way to have default function parameters in C# like we have
I have thread spawning function which accepts many parameters which have default values in
Is there a way to overwrite the default search function in wordpress? I have
i have default.aspx page and one user control. usercontrol is having following code for

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.