Re: returning lvalue in C vs C++



On 2006-03-29, Ben C <spamspam@xxxxxxxxx> wrote:
On 2006-03-29, Phlip <phlip2005@xxxxxxxxx> wrote:
Ben C wrote:

struct thing
{
int x;
};

struct thing f(void)
{
struct thing ret = {100};
return ret;
}

int g(void)
{
return 10;
}

int main(void)
{
#if 0
g() = 4; /* "invalid lvalue" in C and C++ */
#endif
f().x = 200; /* "invalid lvalue" in C, but not in C++ apparently */

Consider the general scheme of C++, and specifically its overloaded
operators.

x.operator=() (figuratively) could have a side-effect that your code depends
on. So regardless where 'thing' appears, thing.x is accessible as an
lvalue.

It would be thing.operator=() but I see the point.

Sorry, no it wouldn't, you were right. It's x's operator= we'd be
calling.

But x is an int, so if f().x = 200 works on that basis, why not g() = 4?
.



Relevant Pages

  • Re: Linux 2.6.25.4
    ... return ret; ... +int sparc_mmap_check ... struct device_node *node, ...
    (Linux-Kernel)
  • Re: [git pull] core fixes
    ... extern void lock_acquire(struct lockdep_map *lock, unsigned int subclass, ... #ifdef CONFIG_DEBUG_LOCK_ALLOC ... int ret; ...
    (Linux-Kernel)
  • [RFC git pull] "big box" x86 changes
    ... the x86 spectrum, small and large boxes alike, so i'd be seriously ... export linked list of struct setup_data via debugfs ... int count; ...
    (Linux-Kernel)
  • Please pull git390 for-linus branch
    ... -static int ipl_init ... struct stack_frame *sf; ... * @offset: byte offset of source value in the array ...
    (Linux-Kernel)
  • Re: Linux 2.6.29.4
    ... static int is_efer_nx ... if (ret) ... struct ftdi_private { ... the specific security attributes of the socket ...
    (Linux-Kernel)