Search code examples
c++c++11lvalue

Of what kind of lvalues can the address not be taken?


In this conference, Scott Meyers starts by saying "lvalues are generally expressions you can take the address of". I am stressing the word generally: what is an lvalue that you cannot take the address of? (if it exists).

EDIT: Please provide code snippets with your answers, it make things clearer.


Solution

  • I think bitfields satisfy your conditions... I believe f.x is an lvalue, but you can't take the address of it. Interestingly, you also can't do auto & x = f.x.

    C++11 Section 8.3.1 Part of Paragraph 4: Since the address of a bit-field (9.6) cannot be taken, a pointer can never point to a bit-field.

    struct foo {
        int x : 3;
        int y : 3;
        int z : 3;
    };
    
    int main() {
        foo f;
    
        f.x = 3;
    }
    

    I'm even less sure about this other idea, but the standard says that you are not allowed to use the main function. If taking the address of main is using it, then that would also suffice. C++11 3.6.1 Paragraph 5.


    And from a comment I left, I know TI extends their C and C++ compilers such that there is a storage class called cregister that you can't take the address of. (Because it refers to a physical register, which doesn't have a concept of an address).