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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:02:46+00:00 2026-06-12T09:02:46+00:00

I came across this bit of code in an example from the Boost documentation:

  • 0

I came across this bit of code in an example from the Boost documentation:

std::vector<int> input;
input += 1,2,3,4,5,6,7,8,9;

How cute. Boost has a template for operator+= that takes advantage of the fact that the comma is, under most circumstances, an operator. (Wisely, C++ does not allow a hackist to overload “operator,“.)

I like to write cute code too, so I played around some with the comma-operator. I found something that looks weird to me. What do you think the following code will print?

#include <iostream>
int main() {
    int i;
    i = 1,2;
    std::cout << i << ' ';
    i = (1,2);
    std::cout << i << std::endl;
}

You guessed it. VC++ 2012 prints “1, 2”. What’s up with that?

[Edit: I should have been more precise. Should have said C++ does not allow operator “,” in a list of int’s to be overloaded. Or better yet, nothing. The ‘,’ operator can be overloaded for classes and enums.]

  • 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-12T09:02:47+00:00Added an answer on June 12, 2026 at 9:02 am

    CASE 1:

    i = 1,2;

    = has higher precedence than ,

    hence, 1 is assigned to i.

    Since assignment evaluates to an lvalue in c++,(evaluates to rvalue in c) it becomes i,2 which evaluates to2 (refer NOTE)

    CASE 2:

    i = (1,2);

    () has higher precedence than =

    expressions or operands separated by , operator evaluates to the value of the last expression or operand hence, 2 is assigned to i


    NOTE

    a comma expression like 33,77,x,y,z is evaluated from left to right.

    The result of such comma expression is the value of rightmost expression .

    Examples

    Consider, int z=100; 
    then
    1,4,5; //evaluates to 5
    1,100,z+100; //evaluates to 200
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently came across this genius bit of code on HTTP POST from PHP,
So I came across this bit of php code: <?php $long_url = http://example.com; $bit_ly
I came across this bit of vba code posted in another SO question. Is
I'm maintaining some existing pages and came across a bit of code from System.Threading
I thought I understood variable scope until I came across this bit of code:
I came across this code: #include<stdio.h> void main() { int x; float t; scanf(%f,&t);
Hi I came across this bit of C++ code and am trying to learn
I came across this bit of code: n = args[0] as Long [*n..1, n].any{
I came across this strange bit of CSS tonight... display: inline !ie; Now I've
I was doing a bit research and I came across this comment. I have

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.