Bug 49003 - [C++0x][DR 1207] decltype in member function's trailing return type should deduce constness of *this
Summary: [C++0x][DR 1207] decltype in member function's trailing return type should de...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.7.0
: P3 normal
Target Milestone: 4.7.0
Assignee: Jason Merrill
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2011-05-15 03:04 UTC by Nathan Ridge
Modified: 2011-06-29 22:20 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2011-05-15 17:19:52


Attachments
patch for 4.6 (1.95 KB, patch)
2011-06-29 22:20 UTC, Jason Merrill
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Nathan Ridge 2011-05-15 03:04:46 UTC
Now that the resolution of issue #1207 (http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1207) made it into the standard, the following example (taken from the same page) should compile:

struct vector {
    struct iterator { };
    struct const_iterator { };
    iterator begin();
    const_iterator begin() const;
};
class block {
    vector v;
    auto end() const -> decltype(v.begin()) { return v.begin(); }
};

Currently, GCC gives the same error as before:

test.cpp: In member function ‘vector::iterator block::end() const’:
test.cpp:9:66: error: could not convert ‘((const block*)this)->block::v.vector::begin()’ to ‘vector::iterator’
Comment 1 Jonathan Wakely 2011-05-15 17:19:52 UTC
I thought there was already a bug report about this but I can't find it, so confirmed
Comment 2 Jonathan Wakely 2011-05-16 11:09:05 UTC
ah I was thinking of PR 45908 (where I provided that reduced testcase - I knew it looked familiar!) but that was for an ICE which is fixed, so this one should be kept for the change in semantics
Comment 3 Jonathan Wakely 2011-05-16 14:48:13 UTC
A simpler test for DR 1207 being implemented is:

struct A {
    auto a() const -> decltype(this) { return this; }
};

nc.cc:2:32: error: invalid use of 'this' at top level
nc.cc:2:32: error: invalid use of 'this' at top level

The late-specified return type should be accepted with the same type as the return statement's expression i.e. const A*
Comment 4 Jason Merrill 2011-06-29 21:19:35 UTC
Author: jason
Date: Wed Jun 29 21:19:31 2011
New Revision: 175671

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=175671
Log:
	DR 1207
	PR c++/49003
	* cp-tree.h (struct saved_scope): Add x_current_class_ptr,
	x_current_class_ref.
	(current_class_ptr, current_class_ref): Use them.
	* decl.c (build_this_parm): Handle getting the class type.
	* parser.c (cp_parser_late_return_type_opt): Set up 'this'
	for use within the trailing return type.

Added:
    trunk/gcc/testsuite/g++.dg/cpp0x/trailing6.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/cp-tree.h
    trunk/gcc/cp/decl.c
    trunk/gcc/cp/parser.c
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/g++.dg/cpp0x/pr45908.C
Comment 5 Jason Merrill 2011-06-29 21:20:14 UTC
Implemented for 4.7.
Comment 6 Nathan Ridge 2011-06-29 21:50:06 UTC
Thanks Jason! Is there any chance of getting this into 4.6.2?
Comment 7 Jason Merrill 2011-06-29 22:20:26 UTC
Created attachment 24642 [details]
patch for 4.6

I've been pretty free about applying non-regression fix patches to the 4.6 branch, but I think now that 4.6.1 has gone out I'm going to be more conservative.  But here's a patch against the 4.6 branch if you want to apply it locally.