Fun with commas

This thread over at GameDev got me thinking, “can one assign Python-like tuples in C++?”

I don’t want to pollute the thread in For Beginners with that discussion, but the answer is yes, even without C++11 initialiser lists:


#include <iostream>

struct A {
    A &operator = (int i) {
        std::cout << "A = " << i << std::flush;

        return *this;
    }

    A &operator , (int i) {
        std::cout << ", " << i << std::flush;

        return *this;
    }
};

int main() {
    A a;

    a = 10, 20, 30;

    std::cout << std::endl;
}

Should you ever do this? Probably not. Though I’m guessing one of Boost’s container libraries is doing exactly this.

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s