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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:14:22+00:00 2026-06-03T21:14:22+00:00

To have a sort of duck typing, I do template<bool b> struct A{ static

  • 0

To have a sort of duck typing, I do

template<bool b>
struct A{
  static template<typename V> f1(V*, [other params]);     
  static template<typename V> f2(V*, [other params]);            
};

template<> template<typename T>
void A<false>::f1(V*, [other params]){}

template<> template<typename T>
void A<true>::f1(V*, [other params]){
   ...some code...
}  

template<int flags>
struct V{
  void f(){
     A<flags&Some compile time conditions>::f1 (this,[params]);
     A<flags&Some compile time conditions>::f2 (this,[params]); 
  } 
};

Do you think there is a more elegant solution, which is not Template class, function specialization
(I do not want to add an extra param to the functions)

I would like to do something like

template<int X> struct C{
 void f(){std::cout<<"C::f"<<std::endl;};
};


template<> struct C<0>{
};


template<int X> struct D{
 C<X> c;

 template<bool b>
 void f();

 void g(){
  f<X!=0>();
 }

};

template<>
template<int X> 
void D<X>::f<true>{
c.f();
};

template<int X>  
 template<>
 void D<X>::f<false>{};


int main(){
 D<3> ch;
 ch.g();

 D<0> cn;
 cn.g();

}

but this is not valid code, and I get error: template-id ‘f’ used as a declarator.

Is there a way to specialize a template function by the value of its non-type template parameter?

  • 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-03T21:14:24+00:00Added an answer on June 3, 2026 at 9:14 pm
    template<>
    template<int X> 
    void D<X>::f<true>(){
    c.f();
    };
    
    template<int X>  
     template<>
     void D<X>::f<false>(){};
    

    That is illegal (all attempts are). When you specialize a member function template, it’s enclosing class has to be specialized as well.

    However, you can overcome that easily by wrapping your function inside a templated struct that would take its template arguments. Something like

    template <int X, bool B>
    struct DXF;
    
    template <int X>
    struct DXF<X, true>
    {
      static void f() { // B is true!
      }
    };
    
    template <int X>
    struct DXF<X, false>
    {
      static void f() { // B is false!
      }
    };
    

    and call it with DXF<X, (X!=0)>::f().

    However, it seems that you just want to specialize for X==0. In that case, you can just specialize:

    template <>
    void D<0>::f() {}
    

    Note that f in that case is not a member template.


    Another option you can go for is overloading. You could wrap your int in a parameter list of some template, like this:

    template<int X> struct D{
     C<X> c;
    
     void f(std::true_type*) { ... true code ... }
     void f(std::false_type_*) { ... false code ... }
     void g(){
      f((std::integral_constant<bool, X!=0>*)0);
     }
    

    Note that true_type and false_type are just typedefs of std::integral_constant<bool, true> and false, resp.

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

Sidebar

Related Questions

I have some sort of recursive function, but I need to parse a string,
I have to sort out my data by some column, such that some specific
I have some sort of a design problem with my Django AJAX application. I
I am looking for some help. I have found this script that sort of
I have some sort of elsif statment bug I can't find: My JSfiddle: http://jsfiddle.net/z3xV3/64/
I've got some troubles with OCMock and UIView. I have sort of this code:
trying to have some sort of gradiented border by having a parent element have
I have to sort a table and looking for right plugin. Some of the
I have some (sort of) Singletons which exist in three context: Per AppDomain Per
I have some sort of problem, when it comes to customizing the iPad-launch-lcreens of

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.