Skip to content

Inline method returning a reference type fails to compile due to incorrect cast to pointer type #1302

Open
@vlad-rw

Description

@vlad-rw

Describe the bug

Disclaimer: Not a C++ expert, please amend the description if incorrect

To Reproduce

#[test]
fn test_reference_const_char_cast() {
    let hdr = indoc! {"
        #include <cstddef>

        class Bytes {
          public:
            typedef char value_type;
            typedef std::size_t size_type;
            typedef const char& reference;
          private:
            const value_type *d_ptr;
            size_type d_length;
          public:
            reference front() const;
        };

        inline Bytes::reference Bytes::front() const
        {
            return d_ptr[0];
        }
    "};
    let rs = quote! {};
    run_test("", hdr, rs, &["Bytes"], &[]);
}

Fails with:

target/cxx/gen0.cxx:137:26: error: cannot initialize a variable of type 'const char *(Bytes::*)() const' with an rvalue of type 'Bytes::reference (Bytes::*)() const': different return type ('const char *' vs 'Bytes::reference' (aka 'const char &'))  char const *(::Bytes::*front$)() const = &::Bytes::front;

This example taken from a vendor library header (and minimised).

Expected behavior

It compiles and returns a reference.

Additional context

Apologies if this is actually the same issue as something else - I tried to search the issues and this seems original.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinghas-test-caseTest case written and checked in

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions