开发者

Declaring a Reference inside Function Parameter List

开发者 https://www.devze.com 2023-04-09 16:32 出处:网络
I\'m trying to do something funky with macros and to do it, I need to do something even funkier. To give an example of what I\'m trying 开发者_Python百科to do, consider the code below:

I'm trying to do something funky with macros and to do it, I need to do something even funkier. To give an example of what I'm trying 开发者_Python百科to do, consider the code below:

#include <iostream>

int set_to_three(int& n) {
    n = 3;
    return 0;
}

int main() {

    int s = set_to_three(int& t); // <-- Obviously this wouldn't compile

    t += 5;

    std::cout << t << std::endl; // <-- This should print 8
    std::cout << s << std::endl; // <-- This should print 0

    return 0;
}

So as you can see, I want to call a function, declare its parameter, and capture the return value of the function in exactly ONE expression. I tried using the comma operator in various funky ways, but no results.

I was wondering if this is at all possible, and if so, how could I do it? I'm thinking it may be possible using comma operators, but I simply don't know how. I'm using Visual Studio 2010, in case you need to know which compiler I'm using.


Since you only have two ints, this will work:

int t, s = set_to_three(t);

Note that this is not comma operator.

Were the types of s and t different, it wouldn't be possible IMHO.

0

精彩评论

暂无评论...
验证码 换一张
取 消