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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T08:56:54+00:00 2026-06-16T08:56:54+00:00

I have template functions, say, template<class T> void mysort(std::vector<T>& vec) { size_t b, m,

  • 0

I have template functions, say,

template<class T> 
void mysort(std::vector<T>& vec)
{
    size_t b, m, e,
    ...
    mysort(vec, b, m, e);
}

template<class T>
void mysort(std::vector<T>& vec, size_t b, size_t m, size_t e)
{
     size_t x, y, z;
     ...
     mysort (vec, x, y, z);
}

The public interface is the one take only a vector reference. I would like to hide the other one, the implementation, so that no client code can do

mysort(vec, a, b, c);

It doesn’t feel right to create a class and make the implementation function private static, I try to use the anonymous namespace,

namespace abc 
{
    namespace 
    {
        template<class T>
        void mysort(std::vector<T>& vec, size_t b, size_t m, size_t e)..
    }

    template<class T> 
    void mysort(std::vector<T>& vec)...

}

It helps, but not exactly a knockout…

#include "mysort.h"

int main()
{
    ...
    abc::mysort(vec, a, b, c); // this won't compile, good
}

however if I change it to:

#include "mysort.h"

using namespace abc;

int main()
{
    ...
    mysort(vec, a, b, c); // it compiles and runs!!
}

I am using gcc Ubuntu 4.4.3-4ubuntu5 on x86_64.
can anyone explain why it compiles with using directive, but not with qualified names, and whether there is a better way to achieve what I want?

  • 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-16T08:56:56+00:00Added an answer on June 16, 2026 at 8:56 am

    The common idiom for this is to create a “detail” namespace, which is intended only for internally-used code:

    namespace abc 
    {
        namespace detail
        {
            template<class T>
            void mysort(std::vector<T>& vec, size_t b, size_t m, size_t e)..
        }
    
        template<class T> 
        void mysort(std::vector<T>& vec)...
    
    }
    

    To answer your question about the unnamed namespace behavior:

    Unnamed namespaces (they’re not called anonymous namespaces) are named a bit weird — they’re unnamed to you, but the compiler actually generates a unique internal name for it. Your example is equivalent to:

    namespace abc 
    {
        namespace def // lets say 'def' is unique.
        {
            template<class T>
            void mysort(std::vector<T>& vec, size_t b, size_t m, size_t e)..
        }
        using namespace def;
    
        template<class T> 
        void mysort(std::vector<T>& vec)...
    
    }
    

    You’ll notice it behaves the same as your unnamed example: you can’t do abc::mysort(vec, 1, 2, 3) here, but you can using namespace abc; mysort(vec, 1, 2, 3).

    This happens because there aren’t two abc::mysorts, only a abc::def::mysort and abc::mysort. When you declared an actual abc::mysort, it hides the one brought in by using namespace def. Note if you comment out the 1-param mysort, you can actually say abc::mysort(vec, 1, 2, 3).

    Because it was hidden, a qualified call to abc::mysort must look at explicitly abc::mysort, and only finds the 1-param version.

    However, with an unqualified call via using namespace abc; mysort(vec, 1, 2, 3), it can use ADL to find any available function which matches.

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

Sidebar

Related Questions

Say I have the following template function: template <class T> void apply(const vector<complex<T> >&
Say I have this: template<typename T, int X> class foo { public: void set(const
I'm converting my fields class read functions into one template function. I have field
I have a template class, say: template<class T> class someClient { void someCallbackA() {foo_->onA();}
Let's say I have the following snippet: template <class T> void f(T arg) {
Say I have a templated Action template <class ArgT> struct Action { Action(::boost::function< void(ArgT)
Say I have a templated class: template <typename T> class foo { void do_someting(T
Say I have : template < typename T > class ClassA { void doTheStuff
Let's assume we have a template function foo: template<class T> void foo(T arg) {
Say I have two Template classes. template<class T> class baseclass1 { template<class> friend class

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.