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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:48:30+00:00 2026-06-01T10:48:30+00:00

Given a template template <int n> void f(){…}; I know I can specialize it

  • 0

Given a template

template <int n>
void f(){...};

I know I can specialize it for specific values of n by doing:

template <>
void f<2>(){...};

But, is there a method which allows me to specialize it for all positive n?

I thought of doing the following

template <int n>
void f<n>(){
    int dummy[n]; //invalid for n < 0
    ...
};

So for n<0 this code is invalid and the compiler would resort to the previous definition. Unfortunately, all I get is a redefinition of 'void f<n>()' error.

Note: I’m guessing this is probably not supported by the standard. I’m asking if there isn’t some method (maybe some template metaprogramming) to achieve this effect.

  • 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-01T10:48:31+00:00Added an answer on June 1, 2026 at 10:48 am

    One option would be to use another level of indirection. Define an auxiliary template that takes in two arguments – the number n and a bool representing whether or not n is negative, then specialize that template for when n is negative. Then, have your f function instantiate the template with the right arguments.

    For example:

    template <int n, bool isNegative> struct fImpl {
        static void f() {
           /* ... code for when n is positive ... */
        }
    };
    template <int n> struct fImpl<n, true> {
        static void f() {
           /* ... code for when n is negative ... */
        }
    };
    
    template <int n> void f() {
        fImpl<n, (n < 0)>::f();
    }
    

    Another option is to use SFINAE overloading and the std::enable_if template class from C++11 (or Boost’s equivalent);

    template <int n> void f(typename std::enable_if<(n < 0)>::type* = 0) {
        /* ... n is negative ... */
    }
    
    template <int n> void f(typename std::enable_if<(n >= 0)>::type* = 0) {
        /* ... n is positive ... */
    }
    

    Each of these functions will only be available for overload resolution if n has the proper sign, so the correct version will always be called.

    Hope this helps!

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

Sidebar

Related Questions

Given: template<class T> struct test { void foo(int b); void testFunc( int a )
//Prints out a given array template <typename T> void print(T t) { for(int i
Consider the following setup: I am given an interface template<class T> void FooClass<T>::foo(boost::function<double (int)>
So given the following template functions with partial specialization template<typename T> void foo(vector<T> &in)
I have method that is returning IQuerable given below: internal IQueryable<TradeLeads> GetLeadsByCategory(int categoryId) {
Is there a way that I can create a function that takes an int
I'm trying to set up a parser which, given a value, can assign it
Given class Foo template <typename T> class Foo { public: ...other methods.. void bar()
Given #include <utility> template <typename T1, typename T2, typename T3> void foo(std::initializer_list<std::pair<T1, T2>> _a,
given the following template function : template <class T> void DoSomething(T &obj1, T &obj2)

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.