In C++11, how do I specify that the implicit "this" parameter "[[carries_dependency]]"?
Asked Answered
Z

1

21

In [dcl.attr.depend]/1, I read:

The attribute[...] carries_dependency [...] may be applied to the declarator-id of a parameter-declaration in a function declaration or lambda, in which case it specifies that the initialization of the parameter carries a dependency to (1.10) each lvalue-to-rvalue conversion (4.1) of that object. The attribute may also be applied to the declarator-id of a function declaration, in which case it specifies that the return value, if any, carries a dependency to the evaluation of the function call expression.

What I'm missing is a way to apply the attribute to the implicit this parameter.

By way of example, consider this free function:

void fun(int i, Foo * [[carries_dependency]] f);

and it's equivalent (but for the attribute) member version:

void Foo::fun(int i); // can't add [[carries_dependency]] here?
Zetta answered 3/7, 2012 at 15:47 Comment(4)
I am quite sure that it can be done, but you will have to look the syntax in the standard. I can try to guess that it will be something like [[carries_dependency]] void fun( int ); or void fun(int) [[carries_dependency]]; in the declaration.Archfiend
@David Rodríguez - dribeas If I'm reading the grammar right the former applies the attribute to the return values of the function (if any) while the latter isn't allowed.Joo
@MarkB: 8.4.1p2 seems to indicate that the function attributes are to be the last elements in the function declaration.Archfiend
I'm curious: what compiler actually implements C++11's attribute syntax?Sixpack
P
3

I'm not certain and don't have a compiler with support for this to test, but here's a swing at a possibility: I think the grammar [gram.decl] indicates that you should be able put it ("attribute-specifier_opt") in the same spot you'd put "const" to indicate a constant this pointer ("cv-qualifier-seq_opt"), which would make sense:

parameters-and-qualifiers:
    ( parameter-declaration-clause ) attribute-specifier_opt cv-qualifier-seq_opt
        ref-qualifier_opt exception-specification_opt

E.g.

struct X{
    void f(int i) [[carries_dependency]];
};
Pygidium answered 10/7, 2012 at 15:32 Comment(1)
Rather unsurprising, when comparing to const members, =default etcSilhouette

© 2022 - 2024 — McMap. All rights reserved.