N3485: 5.1.1.3 [expr.prim.general] Type of `this` in static member function.

閲覧: 138 回
最初の未読メッセージにスキップ

andrew...@gmail.com

未読、
2013/05/13 11:07:562013/05/13
To: std-dis...@isocpp.org
In N3485 5.1.13 [expr.prim.general] it says:

> [this] shall not appear within the declaration of a static member function (although its type and value
category are defined within a static member function

I asked what the relevant or importance of this was on SO here:

http://stackoverflow.com/questions/16524897/type-of-this-in-static-member-function

and someone thought it was a defect.

Basically my question is, of what relevance is the type of `this` inside a static member function, given that it cannot appear there?

Jens Maurer

未読、
2013/05/15 2:19:172013/05/15
To: std-dis...@isocpp.org
On 05/13/2013 05:07 PM, andrew...@gmail.com wrote:
> In N3485 5.1.13 [expr.prim.general] it says:
>
>> [this] shall not appear within the declaration of a static member
>> function (although its type and value
> category are defined within a static member function
[...]
> Basically my question is, of what relevance is the type of `this`
> inside a static member function, given that it cannot appear there?

Sometimes, there are helpful notes in the standard explaining a
potentially obtuse provision. In this case, the note reads:

"[ Note: this is because declaration matching does not occur until
the complete declarator is known. -- end note ]"

If you look at the list of core issues applying to 5.1.1
http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_index.html
there's core issue 945 "Use of this in a late-specified return type"
http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#945
which appears to be superficially related to your question.

And, in fact, that core issue has a cross reference to core issue
1207 "Type of class member in trailing-return-type"
http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1207
which has introduced the phrasing in question.

Does the motivational text in those core issues provide sufficient
insight, or do you need more explanation?

Thanks,
Jens

andrew...@gmail.com

未読、
2013/05/15 6:11:592013/05/15
To: std-dis...@isocpp.org

Thanks Jens, but did you read the response on the SO post?

http://stackoverflow.com/questions/16524897/type-of-this-in-static-member-function

ecatmur refers to 945, 1017 and 1207 and still seems to think that the language is vestigial.  Are you saying that he is wrong?

I'm not sure I understand how the note about declaration matching is relevant here.  Can you provide an example of a situation where the note and the type of `this` being defined within a static member function is relevant?

Is the following what you mean?

struct C
{
    static C* f();
};

auto C::f() -> decltype(this) {}

Is this ill-formed?  Is the note saying that in order to match the function-definition to the declaration inside the class specifier, it is necessary to use the type of `this` to get the signature.  After the declaration match has been made, the compiler notices that the function is static, and then rejects it because `this` can't be used inside a static member function declaration?



Jens Maurer

未読、
2013/05/15 14:15:562013/05/15
To: std-dis...@isocpp.org
On 05/15/2013 12:11 PM, andrew...@gmail.com wrote:
>
>
> On Wednesday, May 15, 2013 8:19:17 AM UTC+2, Jens Maurer wrote:
>
I did not.

> I'm not sure I understand how the note about declaration matching is
> relevant here. Can you provide an example of a situation where the
> note and the type of `this` being defined within a static member
> function is relevant?

I'm thinking about an example where there is a (declaration)
choice between a static and a non-static member function, one of
which is defined out-of-class, similar to the example in the standard
(I've added a missing <class T>):

struct A {
char g();

template<class T> static auto f(T t) -> decltype(t + g())
{ return t + g(); }
};

template<class T> auto A::f(int t) -> decltype(t + g());


I believe you need to have "this" available for the decltype prior to
matching this out-of-line definition with the static declaration.
(At the point where you evaluate the "decltype", you don't know yet
whether you're inside a static member function or not, because you
haven't referred to the declaration inside the class, yet.)

> Is the following what you mean?
>
> struct C { static C* f(); };
>
> auto C::f() -> decltype(this) {}
>
> Is this ill-formed? Is the note saying that in order to match the
> function-definition to the declaration inside the class specifier, it
> is necessary to use the type of `this` to get the signature. After
> the declaration match has been made, the compiler notices that the
> function is static, and then rejects it because `this` can't be used
> inside a static member function declaration?

Yes, I think so. It also occurs with implicit uses of this (9.3.1).
It still a bit strange that you can't use "this" here, though. It's
totally harmless inside decltype.

Jens

Jens Maurer

未読、
2013/05/15 16:08:152013/05/15
To: std-dis...@isocpp.org
Oops, the <class T> was supposed to be missing (an explicit template
instantiation.

Jens
全員に返信
投稿者に返信
転送
新着メール 0 件