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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:43:25+00:00 2026-06-04T05:43:25+00:00

The following #define partially works: #define OUT(x) \ if(x > 0) cout << Hello

  • 0

The following #define partially works:

#define OUT(x) \
   if(x > 0) cout << "Hello "; \
   if(x > 1) cout << x+1

OUT(1) << "message";     // OK

if(0) {
   OUT(1) << "message";     // OK, nothing printed
}

if(0)
   OUT(1) << "message";     // NO, printed anyway

I understand why it does not work (if(0) applies to if(x > 0) only).
I cannot find a way to make it work. Consider that I cannot put braces in the define, otherwise I will not be allowed to use the insertion operator.

  • 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-04T05:43:27+00:00Added an answer on June 4, 2026 at 5:43 am

    Updated to print “Hello”

    It can be done this way (oh the ugliness!):

    #define OUT(x) if((x > 0 ? cout << "Hello " : cout), x > 1) cout << x+1
    

    This is a “standard” comma operator trick that allows additional expressions to be evaluated within the if conditional without affecting the branch taken in the end.

    In this case one expression is added: a ternary operator that evaluates the original condition x > 0. An expression (not a statement, but this restriction does not matter here) that produces the desired side effect is placed in the “true” branch of the ternary. It does not matter at all what the “false” branch evaluates to, so long as it’s the same type as (or can be implicitly converted to) the result of the “true” branch.

    Here the “true” branch returns an ostream&, so the easiest way is to return cout from the “false” branch as well and call it a day.

    Answer to the original question

    In the originally posted case (with x and y) the macro would be

    #define OUT(x) if((x > 0 ? y = x : 0), x > 1) cout << x+1
    

    which for this specific case could be also written as

    #define OUT(x) if((y = x > 0 ? x : y), x > 1) cout << x+1
    

    See it in action.

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

Sidebar

Related Questions

I used the following to get it to work partially: %{ #define OR 2
The following code works fine #define open { #define close } #include<stdio.h> #define int
I'm using following define to make my setup script flexible according to the build
What is the C# replacement for the following define? #define IS_FINITE(x) (0x7FF0 != (*((unsigned
I have the following items (define itemslist (list 'a1 'b2 'c3 (list 'z1 'z2)
I want to define a following function: if(stmtToFinalize) { NSLog(@Finalizing statement stmtToFinalize); if (sqlite3_finalize(stmtToFinalize)
I wish to define the following typeclass Mapping : {-# LANGUAGE MultiParamTypeClasses #-} class
I am using the following CSS to define a custom font on a webpage:
In my Play! 2.0 application I would like to define the following languages: #
I have the following class #ifndef Container_H #define Container_H #include <iostream> using namespace std;

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.