From Jason Turner

[class.conv.fct]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpc17te38q/{from.md → to.md} +55 -12
tmp/tmpc17te38q/{from.md → to.md} RENAMED
@@ -17,22 +17,26 @@ conversion-type-id:
17
  conversion-declarator:
18
  ptr-operator conversion-declaratorₒₚₜ
19
  ```
20
 
21
  specifies a conversion from `X` to the type specified by the
22
- *conversion-type-id*. Such functions are called conversion functions. No
23
- return type can be specified. If a conversion function is a member
24
- function, the type of the conversion function ([[dcl.fct]]) is
25
- “function taking no parameter returning *conversion-type-id*”. A
26
- conversion function is never used to convert a (possibly cv-qualified)
27
- object to the (possibly cv-qualified) same object type (or a reference
28
- to it), to a (possibly cv-qualified) base class of that type (or a
29
- reference to it), or to (possibly cv-qualified) void.[^2]
 
 
 
30
 
31
  ``` cpp
32
  struct X {
33
  operator int();
 
34
  };
35
 
36
  void f(X a) {
37
  int i = int(a);
38
  i = (int)a;
@@ -41,16 +45,20 @@ void f(X a) {
41
  ```
42
 
43
  In all three cases the value assigned will be converted by
44
  `X::operator int()`.
45
 
 
 
46
  A conversion function may be explicit ([[dcl.fct.spec]]), in which case
47
  it is only considered as a user-defined conversion for
48
  direct-initialization ([[dcl.init]]). Otherwise, user-defined
49
  conversions are not restricted to use in assignments and
50
  initializations.
51
 
 
 
52
  ``` cpp
53
  class Y { };
54
  struct Z {
55
  explicit operator Y() const;
56
  };
@@ -67,25 +75,60 @@ void g(X a, X b) {
67
  if (a) {
68
  }
69
  }
70
  ```
71
 
 
 
72
  The *conversion-type-id* shall not represent a function type nor an
73
  array type. The *conversion-type-id* in a *conversion-function-id* is
74
- the longest possible sequence of *conversion-declarator*s. This prevents
75
- ambiguities between the declarator operator \* and its expression
76
- counterparts.
 
 
 
 
 
 
77
 
78
  ``` cpp
79
  &ac.operator int*i; // syntax error:
80
  // parsed as: &(ac.operator int *)i
81
  // not as: &(ac.operator int)*i
82
  ```
83
 
84
  The `*` is the pointer declarator and not the multiplication operator.
85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86
  Conversion functions are inherited.
87
 
88
  Conversion functions can be virtual.
89
 
90
- Conversion functions cannot be declared `static`.
 
 
 
 
 
 
 
 
 
 
 
 
 
91
 
 
17
  conversion-declarator:
18
  ptr-operator conversion-declaratorₒₚₜ
19
  ```
20
 
21
  specifies a conversion from `X` to the type specified by the
22
+ *conversion-type-id*. Such functions are called *conversion functions*.
23
+ A *decl-specifier* in the *decl-specifier-seq* of a conversion function
24
+ (if any) shall be neither a *defining-type-specifier* nor `static`. The
25
+ type of the conversion function ([[dcl.fct]]) is “function taking no
26
+ parameter returning *conversion-type-id*”. A conversion function is
27
+ never used to convert a (possibly cv-qualified) object to the (possibly
28
+ cv-qualified) same object type (or a reference to it), to a (possibly
29
+ cv-qualified) base class of that type (or a reference to it), or to
30
+ (possibly cv-qualified) void.[^2]
31
+
32
+ [*Example 1*:
33
 
34
  ``` cpp
35
  struct X {
36
  operator int();
37
+ operator auto() -> short; // error: trailing return type
38
  };
39
 
40
  void f(X a) {
41
  int i = int(a);
42
  i = (int)a;
 
45
  ```
46
 
47
  In all three cases the value assigned will be converted by
48
  `X::operator int()`.
49
 
50
+ — *end example*]
51
+
52
  A conversion function may be explicit ([[dcl.fct.spec]]), in which case
53
  it is only considered as a user-defined conversion for
54
  direct-initialization ([[dcl.init]]). Otherwise, user-defined
55
  conversions are not restricted to use in assignments and
56
  initializations.
57
 
58
+ [*Example 2*:
59
+
60
  ``` cpp
61
  class Y { };
62
  struct Z {
63
  explicit operator Y() const;
64
  };
 
75
  if (a) {
76
  }
77
  }
78
  ```
79
 
80
+ — *end example*]
81
+
82
  The *conversion-type-id* shall not represent a function type nor an
83
  array type. The *conversion-type-id* in a *conversion-function-id* is
84
+ the longest sequence of tokens that could possibly form a
85
+ *conversion-type-id*.
86
+
87
+ [*Note 1*:
88
+
89
+ This prevents ambiguities between the declarator operator `*` and its
90
+ expression counterparts.
91
+
92
+ [*Example 3*:
93
 
94
  ``` cpp
95
  &ac.operator int*i; // syntax error:
96
  // parsed as: &(ac.operator int *)i
97
  // not as: &(ac.operator int)*i
98
  ```
99
 
100
  The `*` is the pointer declarator and not the multiplication operator.
101
 
102
+ — *end example*]
103
+
104
+ This rule also prevents ambiguities for attributes.
105
+
106
+ [*Example 4*:
107
+
108
+ ``` cpp
109
+ operator int [[noreturn]] (); // error: noreturn attribute applied to a type
110
+ ```
111
+
112
+ — *end example*]
113
+
114
+ — *end note*]
115
+
116
  Conversion functions are inherited.
117
 
118
  Conversion functions can be virtual.
119
 
120
+ A conversion function template shall not have a deduced return type (
121
+ [[dcl.spec.auto]]).
122
+
123
+ [*Example 5*:
124
+
125
+ ``` cpp
126
+ struct S {
127
+ operator auto() const { return 10; } // OK
128
+ template<class T>
129
+ operator auto() const { return 1.2; } // error: conversion function template
130
+ };
131
+ ```
132
+
133
+ — *end example*]
134