Bug 67961 - Incorrect type of meber of struct in error message
Summary: Incorrect type of meber of struct in error message
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 5.2.0
: P3 minor
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-10-14 08:48 UTC by other+gcc
Modified: 2015-10-15 09:11 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments
Test case (130 bytes, text/x-csrc)
2015-10-14 08:48 UTC, other+gcc
Details

Note You need to log in before you can comment on or make changes to this bug.
Description other+gcc 2015-10-14 08:48:48 UTC
Created attachment 36506 [details]
Test case

Compiling following code produces incorrect error message:

#include <cstdint>

void foo(std::size_t &f) {
        f = 5;
}

struct A {
        uint32_t val;
};

int main() {
        A a, *aptr = &a;
        foo(aptr->val);
        return 0;
}

g++ --std=c++11 foo.cpp gives error message:
error: invalid initialization of non-const reference of type ‘std::size_t& {aka long unsigned int&}’ from an rvalue of type ‘std::size_t {aka long unsigned int}’
  foo(aptr->val);
            ^
where type of that rvalue variable is uint32_t not std::size_t. I think I should add that system is 64bit so std::size_t has 64 bits.
Comment 1 Jonathan Wakely 2015-10-14 14:14:44 UTC
The diagnostic is correct.

Trying to pass uint32_t to a function taking size_t causes a conversion, creating a temporary (i.e. rvalue) size_t, but a non-const reference cannot bind to a temporary.
Comment 2 Jonathan Wakely 2015-10-14 14:26:34 UTC
(In reply to other+gcc from comment #0)
> where type of that rvalue variable is uint32_t not std::size_t.

There is no rvalue of type uint32_t. The type of the **lvalue** is uint32_t.

The rvalue is the temporary that gets created implicitly, which is of type size_t.


FWIW clang doesn't mention rvalues, it just says there is no matching function for the call because ...

prog.cc:4:6: note: candidate function not viable: no known conversion from 'std::uint32_t' (aka 'unsigned int') to 'std::size_t &' (aka 'unsigned long &') for 1st argument
Comment 3 Paolo Carlini 2015-10-15 09:11:01 UTC
Closing then.