• No results found

C++-dependent conversion functions

In document C++ CLI Standard pdf (Page 67-70)

13. Variables

14.8 Naming conventions

14.8.2 C++-dependent conversion functions

If a conversion function does not match the criteria for CLS compliance, as listed in §14.8, the conversion function is C++-dependent. The names in Table 14-1 are also used for C++-dependent conversion functions in an assembly.

Both op_Implicit and op_Explicit are allowed to be overloaded on their return type.

Converting constructors are emitted as constructors, never as converting functions. (Constructors in CLI 20

15. Expressions

15.1 Function members

The following function member kinds are added to those defined by Standard C++:

• Properties (both scalar and default indexed)

• Events 5

The statements contained in these function members are executed through function member invocations. The actual syntax for writing a function member invocation depends on the particular function member category. Invocations of default indexed properties employ overload resolution to determine which of a candidate set of function members to invoke.

[Note: The following table summarizes the processing that takes place in constructs involving these three 10

categories of function members that can be explicitly invoked. In the table, e, x, y, and value indicate

expressions classified as variables or values, T indicates an expression classified as a type, F is the simple

name of a function, and P is the simple name of a property.

Construct Example Description

P P::get() Property access P = value P::set(value) E += value E::add(value) Event access E -= value E::remove(value) e[x, y] E::get(x, y)

Default indexed property access

e[x, y] = value E::set(x, y, value)

15

The rewrite rules for e[x] (default indexed accesses) are different where there is only one index. This is because there is a potential ambiguity with the C++ operator[]. Is this mentioned elsewhere? [[#35]] end note]

15.2 Primary expressions

To accommodate the addition of properties, the “Primary expressions” subclause of the C++ Standard (§5.1) 20

has been extended, as follows:

“A static property or event is not associated with any instance of a class, and a program is ill-formed if it refers to this in the accessor functions of a static property or event.”

“An instance property or event is associated with a specific instance of a class, and that instance can refer to this in the accessor functions of that instance property or event.”

25

15.3 Postfix expressions

To accommodate the addition of default indexed properties and Arrays (which are accessed using subscript- like expressions), the C++ Standard grammar (§5.2) for postfix-expression has been extended, as follows:

Expressions

postfix-expression: ...

postfix-expression [ expression ]

indexed-access

Indexed access is described in §15.3.2. 5

15.3.1 Subscripting

Given a class instance X, of a type having a default indexed property and operator[], an expression of the

form X[i] is ambiguous. In such cases, the operator[] function or default indexed property accessor

function must be called directly, as appropriate. If a derived class defines only one of operator[] or a

default indexed property, lookup will use that function rather than making the program ambiguous. 10

15.3.2 Indexed access

An indexed-access consists of an indexed-designator, followed by a “[” token, followed by an expression-

list, followed by a “]” token. The expression-list consists of one or more expressions, separated by commas.

indexed-access:

indexed-designator [ expression-list ]

15

indexed-designator shall designate an instance that has one or more default indexed properties that are applicable with respect to the expression-list of the indexed-access.

An indexed-access is interpreted as follows: Each default indexed property with only one indexing

parameter has an associated operator[] synthesized. For the property property int default[int],

the synthesized “operator[](int)” is created. Overload resolution for the appropriate operator[] is

20

done for indexed-access expressions where the expression list is not comma-separated. If a class has two

operator[] operators with the same signature, the expression is ambiguous and the program is ill-formed. Otherwise, the rewrite rules for properties and events are used for indexed-access expressions.

Need to consider how these expressions are interpreted in templates. [[#111]]

Commas in expression-list are treated as a special case—they are considered punctuators. However, if an 25

expression in that list is enclosed in parentheses, any commas inside that expression are interpreted as operators (and behave as described in §5.18/2 of the C++ Standard).

struct S {

property int default[int index] { … } // indexed property

1

30

property int default[string idx1, int idx2] { … } // indexed property 2

};

void f(S& s, string& x, int j) {

s[x,j] = 42; // ok, uses indexed property 2

35

s[1,j] = 42; // error (tries to use indexed property 2,

// but there is a type mismatch;

// no comma operator is used)

s[(1,j)] = 42; // ok, uses indexed property 1 with j as the

argument

40

s[(1,x),j] = 42; // ok, uses indexed property 2 }

[Note: Given a class instance X, of a type having a default indexed property and operator[], an expression

of the form X[i] can be ambiguous. In such cases, the operator[] function or default indexed property

accessor function must be called directly, as appropriate. end note] 45

15.3.3 Function call

Add text to indicate the circumstances under which the following type modifiers shall be emitted, and point to each modifier's definition:

• IsByValue (i.e., ref class type passed by value).

• IsConst (i.e., pointer or reference to a const-qualified type).

• IsExplicitlyDereferenced (i.e., interior_ptr as a parameter).

• IsImplicitlyDereferenced (i.e., parameter is a reference).

• IsLong (i.e., long/unsigned long/long double parameters). 5

• IsExplicitlyDereferenced (i.e., pin_ptr as a parameter).

• IsSignUnspecifiedByte (i.e., plain char's sigedness).

• IsUdtReturn (i.e., ref class type returned by value).

• IsVolatile (i.e., pointer or reference to a volatile-qualified type).[[#131]]

The C++ Standard (§5.2.2/1) states, “A function call is a postfix expression followed by parentheses 10

containing a possibly empty, comma-separated list of expressions which constitute the arguments to the function.”

C++/CLI contains support for delegates (§26). As such, the postfix expression can be a delegate type, in which case, the whole expression is a delegate invocation (§26.3), and the argument list is passed to each function encapsulated by the delegate.

15

15.3.4 Explicit type conversion (functional notation)

In document C++ CLI Standard pdf (Page 67-70)