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

  • Home
  • SEARCH
  • 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 659987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:06:23+00:00 2026-05-13T23:06:23+00:00

As most C++ programmers should know, partial template specialization of free functions is disallowed.

  • 0

As most C++ programmers should know, partial template specialization of free functions is disallowed. For example, the following is illegal C++:

template <class T, int N>
T mul(const T& x) { return x * N; }

template <class T>
T mul<T, 0>(const T& x) { return T(0); }

// error: function template partial specialization ‘mul<T, 0>’ is not allowed

However, partial template specialization of classes/structs is allowed, and can be exploited to mimic the functionality of partial template specialization of free functions. For example, the target objective in the last example can be achieved by using:

template <class T, int N>
struct mul_impl
{
    static T fun(const T& x) { return x * N; }
};

template <class T>
struct mul_impl<T, 0>
{
    static T fun(const T& x) { return T(0); }
};

template <class T, int N>
T mul(const T& x)
{
    return mul_impl<T, N>::fun(x);
}

It’s more bulky and less concise, but it gets the job done — and as far as users of mul are concerned, they get the desired partial specialization.


My questions is: when writing templated free functions (that are intended to be used by others), should you automatically delegate the implementation to a static method function of a class, so that users of your library may implement partial specializations at will, or do you just write the templated function the normal way, and live with the fact that people won’t be able to specialize them?

  • 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-13T23:06:24+00:00Added an answer on May 13, 2026 at 11:06 pm

    As litb says, ADL is superior where it can work, which is basically whenever the template parameters can be deduced from the call parameters:

    #include <iostream>
    
    namespace arithmetic {
        template <class T, class S>
        T mul(const T& x, const S& y) { return x * y; }
    }
    
    namespace ns {
        class Identity {};
    
        // this is how we write a special mul
        template <class T>
        T mul(const T& x, const Identity&) {
            std::cout << "ADL works!\n";
            return x;
        }
    
        // this is just for illustration, so that the default mul compiles
        int operator*(int x, const Identity&) {
            std::cout << "No ADL!\n";
            return x;
        }
    }
    
    int main() {
        using arithmetic::mul;
        std::cout << mul(3, ns::Identity()) << "\n";
        std::cout << arithmetic::mul(5, ns::Identity());
    }
    

    Output:

    ADL works!
    3
    No ADL!
    5
    

    Overloading+ADL achieves what you would have achieved by partially specializing the function template arithmetic::mul for S = ns::Identity. But it does rely on the caller to call it in a way which allows ADL, which is why you never call std::swap explicitly.

    So the question is, what do you expect users of your library to have to partially specialize your function templates for? If they’re going to specialize them for types (as is normally the case with algorithm templates), use ADL. If they’re going to specialize them for integer template parameters, as in your example, then I guess you have to delegate to a class. But I don’t normally expect a third party to define what multiplication by 3 should do – my library will do all the integers. I could reasonably expect a third party to define what multiplication by an octonion will do.

    Come to think of it, exponentiation might have been a better example for me to use, since my arithmetic::mul is confusingly similar to operator*, so there’s no actual need to specialize mul in my example. Then I’d specialize/ADL-overload for the first parameter, since “Identity to the power of anything is Identity”. Hopefully you get the idea, though.

    I think there is a downside to ADL – it effectively flattens namespaces. If I want to use ADL to “implement” both arithmetic::sub and sandwich::sub for my class, then I could be in trouble. I don’t know what the experts have to say about that.

    By which I mean:

    namespace arithmetic {
        // subtraction, returns the difference of lhs and rhs
        template<typename T>
        const T sub(const T&lhs, const T&rhs) { return lhs - rhs; }
    }
    
    namespace sandwich {
        // sandwich factory, returns a baguette containing lhs and rhs
        template<typename SandwichFilling>
        const Baguette sub(const SandwichFilling&lhs, const SandwichFilling&rhs) { 
          // does something or other 
        }
    }
    

    Now, I have a type ns::HeapOfHam. I want to take advantage of std::swap-style ADL to write my own implementation of arithmetic::sub:

    namespace ns {
        HeapOfHam sub(const HeapOfHam &lhs, const HeapOfHam &rhs) {
            assert(lhs.size >= rhs.size && "No such thing as negative ham!");
            return HeapOfHam(lhs.size - rhs.size);
        }
    }
    

    I also want to take advantage of std::swap-style ADL to write my own implementation of sandwich::sub:

    namespace ns {
        const sandwich::Baguette sub(const HeapOfHam &lhs, const HeapOfHam &rhs) {
            // create a baguette, and put *two* heaps of ham in it, more efficiently
            // than the default implementation could because of some special
            // property of heaps of ham.
        }
    }
    

    Hang on a minute. I can’t do that, can I? Two different functions in different namespaces with the same parameters and different return types: not usually a problem, that’s what namespaces are for. But I can’t ADL-ify them both. Possibly I’m missing something really obvious.

    Btw, in this case I could just fully specialize each of arithmetic::sub and sandwich::sub. Callers would using one or the other, and get the right function. The original question talks about partial specialization, though, so can we pretend that specialization is not an option, without me actually making HeapOfHam a class template?

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

Sidebar

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.