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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T19:26:02+00:00 2026-05-10T19:26:02+00:00

Is there an elegant way to specialize a template based on one of its

  • 0

Is there an elegant way to specialize a template based on one of its template parameters?

Ie.

template<int N> struct Junk {     static int foo() {         // stuff         return Junk<N - 1>::foo();     } };  // compile error: template argument '(size * 5)' involves template parameter(s) template<int N> struct Junk<N*5> {     static int foo() {         // stuff         return N;     } };  template<> struct Junk<0> {     static int foo() {         // stuff         return 0;     } }; 

Ie. I am trying to specialize a template based on the parameter being divisible by 5. The only way I can seem to do it is like below:

template<int N> struct JunkDivisibleBy5 {     static int foo() {         // stuff         return N;     } };  template<int N> struct Junk {     static int foo() {         // stuff         if ((N - 1) % 5 == 0 && N != 1)             return JunkDivisibleBy5<N - 1>::foo();         else             return Junk<N - 1>::foo();     } };   template<> struct Junk<0> {     static int foo() {         // stuff         return 0;     } }; 

But this is significantly less elegant, and also necessitates instantiation of all templates even if the template argument shouldn’t require it.

  • 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. 2026-05-10T19:26:03+00:00Added an answer on May 10, 2026 at 7:26 pm

    How’s this:

    #include <iostream> using namespace std;  template < typename T, T N, T D > struct fraction {     typedef T value_type;     static const value_type num = N;     static const value_type denom = D;     static const bool is_div = (num % denom == 0); };  template< typename T, T N, T D, bool P > struct do_if {     static void op() { cout << N << ' NOT divisible by ' << D << endl; } };  template< typename T, T N, T D > struct do_if< T, N, D, true > {     static void op() { cout << N << ' divisible by ' << D << endl; } };  template < int N > void foo() {     typedef fraction< int, N, 5 > f;     do_if< typename f::value_type, f::num, f::denom, f::is_div >::op(); }  int main() {     foo< -5 >();     foo< -1 >();     foo< 0 >();     foo< 1 >();     foo< 5 >();     foo< 10000005 >();     return 0; } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 66k
  • Answers 66k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer The 'a:hover' literally tells the browser to change the properties… May 11, 2026 at 11:26 am
  • added an answer Since you specifically ask about MFC, I assume you mean… May 11, 2026 at 11:26 am
  • added an answer I think there's a few more steps to accomplishing your… May 11, 2026 at 11:26 am

Related Questions

Is there an elegant way to specialize a template based on one of its
Is there an elegant way to have performant, natural sorting in a MySQL database?
Is there an elegant way to create and initialize a const std::vector<const T> like
Is there an elegant way to handle exceptions that are thrown in finally block?
In Java, is there an elegant way to detect if an exception occurred prior
Is there an elegant way in Perl to find the newest file in a
I was just wondering if there is an elegant way to set the maximum
Is there some elegant way to add an empty option to a DropDownList bound
Is there an elegant solution for synchronizing sessions across multiple ASP.NET applications? I'm sure
In c# (3.0 or 3.5, so we can use lambdas), is there an elegant

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.