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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:17:49+00:00 2026-06-01T02:17:49+00:00

Problem in short: How could one implement static if functionality, proposed in c++11, in

  • 0

Problem in short:
How could one implement static if functionality, proposed in c++11, in plain c++ ?

History and original problem:
Recently I came up with a problem like this. I need a class Sender with an interface like

class Sender
{
   void sendMessage( ... );
   void sendRequest( ... );
   void sendFile( ... );
   // lots of different send methods, not important actually
}

In some cases I will need to create a DoubleSender, i.e. an instance of this class, which would call its methods twice, i.e. when calling, let’s say, a sendMessage(...) method, the same message has to be sent twice.

My solutions:
First approach:
Have an isDouble member, and in the end of each method call make a check

sendMessage(...) { ... if( isDouble ) { sendMessage( ... ); }

Well, I don’t want this, because actually I will need double posting very recently, and this part of code in time-critical section will be 98% passive.

Second approach:
Inherit a class DoubleSender from Sender, and implement its methods like:

void DoubleSender::sendMessage( ... )
{
   Sender::sendMessage(...);
   Sender::sendMessage(...);
}

Well, this is acceptable, but takes much space of unpleasant code (really much, because there are lots of different send.. methods.

Third approach:
Imagine that I am using c++11 :). Then I can make this class generic and produce the necessary part of code according to tempalte argument using static if:

enum SenderType { Single, Double };
template<SenderType T>
class Sender
{
   void sendMessage(...)
   {
      // do stuff
      static if ( T == Single )
      {
         sendMessage(...);
      }
   }
};

This is shorter, easier to read than previous solutions, does not generate additional code and… it’s c++11, which I unfortunately cannot use in my work.

So, here is where I came to my question – how can I implement static if analog in c++ ?
Also, I would appreciate any other suggestions about how to solve my original problem.
Thanks in advance.

  • 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-01T02:17:51+00:00Added an answer on June 1, 2026 at 2:17 am

    Quoting @JohannesSchaubLitb

    with my static_if that works on gcc one can do it 🙂

    in some limited fashion

    (see also here)

    This trick involves a specific GCC interpretation of the specs on Lambdas in C++11. As such, it will (likely) become a defect report against the standard. This will lead to the trick no longer working in more recent version of GCC (it already doesn’t work in 4.7).

    See the comment thread below for some more details from Johanness

    http://ideone.com/KytVv:

    #include <iostream>
     
    namespace detail {
    template<bool C>
    struct call_if { template<typename F> void operator<<(F) { } };
     
    template<>
    struct call_if<true> {
      template<typename F>
      void operator<<(F f) { f(); }
    };
    }
     
    #define static_if(cond) detail::call_if<cond>() << [&]
     
    template<bool C, typename T>
    void f(T t) {
      static_if(C) {
        t.foo();
      };
    }
     
    int main() {
      f<false>(42);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So the wonderful low down on this doozie of a problem: short version: We
Short problem: #include <iostream> using namespace std; int main() { double **T; long int
SHORT DESCRIPTION OF PROBLEM: I want to set the text of a searchbar without
Following problem: I want to render a news stream of short messages based on
i have one problem when i sort the NSMutablearray using date and time wise.
I'm hoping someone has run into this sort of problem before, and can give
I have a re-occurring design problem with certain classes which require one-off initialization with
I've got a serious problem with my GPS on my Nexus One: I wrote
we've implemented a system similar to the one described in this other SO post
This is a problem I've had to deal with in my last project, and

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.