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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:53:16+00:00 2026-05-26T21:53:16+00:00

I use a template parameter to determine if a certain behavior must be done

  • 0

I use a template parameter to determine if a certain behavior must be done or not. But this code generate a warning on VS2008 : Warning 26 warning C4127: conditional expression is constant

Here an exemple of the code :

template <class param, bool param2=true>
class superclass1
{
public:
  int foo()
  {
     if(param2)
        doSomthingMore();

     return 1;
   }
};

Is there a way to tranform the code to remove the warning and get the same features?

  • 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-26T21:53:17+00:00Added an answer on May 26, 2026 at 9:53 pm

    This is done via partial specialization. The crudest version looks like this:

    template <typename, bool> class superclass1;
    
    template <class param> class superclass1<param, true>
    class superclass1
    {
    public:
      int foo()
      {
        doSomthingMore();
        return 1;
      }
    };
    
    template <class param> class superclass1<param, false>
    class superclass1
    {
    public:
      int foo()
      {
        return 1;
      }
    };
    

    A more sophisticated approach might declare a member template function and only specialize that. Here’s a solution with auxiliary tag classes:

    #include <type_traits>
    
    template <bool B> class Foo
    {
      struct true_tag {};
      struct false_tag {};
      void f_impl(true_tag = true_tag()){}     // your code here...
      void f_impl(false_tag = false_tag()){}   // ... and here
    
    public:
      void foo()
      {
        f(typename std::conditional<B, true_tag, false_tag>::type());
      }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a template function, that does not use the template parameter for input directly.
I'm trying to use an instant of a custom class as a template parameter.
Possible Duplicate: How to use enable_if to enable member functions based on template parameter
I'm looking forward to use Symfony2. But i dont want to use a template
Is it possible to use a non-type constant template parameter in a preprocessor directive?
How to use lambda expression as a template parameter? E.g. as a comparison class
a template parameter can be used in another template parameter that follows it this
I often get into situation when I'd like to use template method pattern, but
When I try to use float as a template parameter, the compiler cries for
I have a class template that intends to use its parameter K as the

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.