[class.conv.fct]

# 11 Classes [[class]](./#class)

## 11.4 Class members [[class.mem]](class.mem#class.conv.fct)

### 11.4.8 Conversions [[class.conv]](class.conv#fct)

#### 11.4.8.3 Conversion functions [class.conv.fct]

[conversion-function-id:](#nt:conversion-function-id "11.4.8.3 Conversion functions [class.conv.fct]")  
operator [*conversion-type-id*](#nt:conversion-type-id "11.4.8.3 Conversion functions [class.conv.fct]")

[conversion-type-id:](#nt:conversion-type-id "11.4.8.3 Conversion functions [class.conv.fct]")  
[*type-specifier-seq*](dcl.type.general#nt:type-specifier-seq "9.2.9.1 General [dcl.type.general]") [*conversion-declarator*](#nt:conversion-declarator "11.4.8.3 Conversion functions [class.conv.fct]")opt

[conversion-declarator:](#nt:conversion-declarator "11.4.8.3 Conversion functions [class.conv.fct]")  
[*ptr-operator*](dcl.decl.general#nt:ptr-operator "9.3.1 General [dcl.decl.general]") [*conversion-declarator*](#nt:conversion-declarator "11.4.8.3 Conversion functions [class.conv.fct]")opt

[1](#1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L2563)

A declaration
whose [*declarator-id*](dcl.decl.general#nt:declarator-id "9.3.1 General [dcl.decl.general]") has
an [*unqualified-id*](expr.prim.id.unqual#nt:unqualified-id "7.5.5.2 Unqualified names [expr.prim.id.unqual]") that is a [*conversion-function-id*](#nt:conversion-function-id "11.4.8.3 Conversion functions [class.conv.fct]") declares a [*conversion function*](#def:function,conversion "11.4.8.3 Conversion functions [class.conv.fct]");
its [*declarator*](dcl.decl.general#nt:declarator "9.3.1 General [dcl.decl.general]") shall be
a function declarator ([[dcl.fct]](dcl.fct "9.3.4.6 Functions")) of the form

[*noptr-declarator*](dcl.decl.general#nt:noptr-declarator "9.3.1 General [dcl.decl.general]") [*parameters-and-qualifiers*](dcl.decl.general#nt:parameters-and-qualifiers "9.3.1 General [dcl.decl.general]")

where the [*noptr-declarator*](dcl.decl.general#nt:noptr-declarator "9.3.1 General [dcl.decl.general]") consists solely of
an [*id-expression*](expr.prim.id.general#nt:id-expression "7.5.5.1 General [expr.prim.id.general]"),
an optional [*attribute-specifier-seq*](dcl.attr.grammar#nt:attribute-specifier-seq "9.13.1 Attribute syntax and semantics [dcl.attr.grammar]"), and
optional surrounding parentheses, and
the [*id-expression*](expr.prim.id.general#nt:id-expression "7.5.5.1 General [expr.prim.id.general]") has one of the following forms:

- [(1.1)](#1.1)

in a [*member-declaration*](class.mem.general#nt:member-declaration "11.4.1 General [class.mem.general]") that belongs to
the [*member-specification*](class.mem.general#nt:member-specification "11.4.1 General [class.mem.general]") of a class or class template
but is not a friend declaration ([[class.friend]](class.friend "11.8.4 Friends")),
the [*id-expression*](expr.prim.id.general#nt:id-expression "7.5.5.1 General [expr.prim.id.general]") is a [*conversion-function-id*](#nt:conversion-function-id "11.4.8.3 Conversion functions [class.conv.fct]");

- [(1.2)](#1.2)

otherwise, the [*id-expression*](expr.prim.id.general#nt:id-expression "7.5.5.1 General [expr.prim.id.general]") is a [*qualified-id*](expr.prim.id.qual#nt:qualified-id "7.5.5.3 Qualified names [expr.prim.id.qual]") whose [*unqualified-id*](expr.prim.id.unqual#nt:unqualified-id "7.5.5.2 Unqualified names [expr.prim.id.unqual]") is a [*conversion-function-id*](#nt:conversion-function-id "11.4.8.3 Conversion functions [class.conv.fct]")[.](#1.sentence-1)

[2](#2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L2589)

A conversion function shall have no non-object parameters and
shall be a non-static member function of a class or class template X;
its declared return type is the [*conversion-type-id*](#nt:conversion-type-id "11.4.8.3 Conversion functions [class.conv.fct]") and
it specifies a conversion from X to
the type specified by the [*conversion-type-id*](#nt:conversion-type-id "11.4.8.3 Conversion functions [class.conv.fct]"),
interpreted as a [*type-id*](dcl.name#nt:type-id "9.3.2 Type names [dcl.name]") ([[dcl.name]](dcl.name "9.3.2 Type names"))[.](#2.sentence-1)

A [*decl-specifier*](dcl.spec.general#nt:decl-specifier "9.2.1 General [dcl.spec.general]") in the [*decl-specifier-seq*](dcl.spec.general#nt:decl-specifier-seq "9.2.1 General [dcl.spec.general]") of a conversion function (if any) shall not be
a [*defining-type-specifier*](dcl.type.general#nt:defining-type-specifier "9.2.9.1 General [dcl.type.general]")[.](#2.sentence-2)

[3](#3)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L2600)

[*Note [1](#note-1)*:

A conversion function is never invoked for
implicit or explicit conversions of an object
to the same object type (or a reference to it),
to a base class of that type (or a reference to it),
or to cv void[.](#3.sentence-1)

Even though never directly called to perform a conversion,
such conversion functions can be declared and can potentially
be reached through a call to a virtual conversion function in a base class[.](#3.sentence-2)

â *end note*]

[*Example [1](#example-1)*: struct X {operator int(); operator auto() -> short; // error: trailing return type};

void f(X a) {int i = int(a);
 i = (int)a;
 i = a;}

In all three cases the value assigned will be converted byX​::​operator int()[.](#3.sentence-3)

â *end example*]

[4](#4)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L2628)

A conversion function may be explicit ([[dcl.fct.spec]](dcl.fct.spec "9.2.3 Function specifiers")), in which case it is only considered as a user-defined conversion for direct-initialization ([[dcl.init]](dcl.init "9.5 Initializers"))[.](#4.sentence-1)

Otherwise, user-defined conversions are not restricted to use in assignments and initializations[.](#4.sentence-2)

[*Example [2](#example-2)*: class Y { };struct Z {explicit operator Y() const;};

void h(Z z) { Y y1(z); // OK, direct-initialization Y y2 = z; // error: no conversion function candidate for copy-initialization Y y3 = (Y)z; // OK, cast notation}void g(X a, X b) {int i = (a) ? 1+a : 0; int j = (a&&b) ? a+b : i; if (a) {}} â *end example*]

[5](#5)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L2652)

The[*conversion-type-id*](#nt:conversion-type-id "11.4.8.3 Conversion functions [class.conv.fct]") shall not represent a function type nor an array type[.](#5.sentence-1)

The[*conversion-type-id*](#nt:conversion-type-id "11.4.8.3 Conversion functions [class.conv.fct]") in a[*conversion-function-id*](#nt:conversion-function-id "11.4.8.3 Conversion functions [class.conv.fct]") is the longest sequence of
tokens that could possibly form a [*conversion-type-id*](#nt:conversion-type-id "11.4.8.3 Conversion functions [class.conv.fct]")[.](#5.sentence-2)

[*Note [2](#note-2)*:

This prevents ambiguities between the declarator operator * and its expression
counterparts[.](#5.sentence-3)

[*Example [3](#example-3)*: &ac.operator int*i; // syntax error:// parsed as: &(ac.operator int *)i// not as: &(ac.operator int)*i

The * is the pointer declarator and not the multiplication operator[.](#5.sentence-4)

â *end example*]

This rule also prevents ambiguities for attributes[.](#5.sentence-5)

[*Example [4](#example-4)*: operator int [[noreturn]] (); // error: noreturn attribute applied to a type â *end example*]

â *end note*]

[6](#6)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L2681)

[*Note [3](#note-3)*:

A conversion function in a derived class hides only
conversion functions in base classes that convert to the same type[.](#6.sentence-1)

A conversion function template with a dependent return type hides only
templates in base classes that correspond to it ([[class.member.lookup]](class.member.lookup "6.5.2 Member name lookup"));
otherwise, it hides and is hidden as a non-template function[.](#6.sentence-2)

Function overload resolution ([[over.match.best]](over.match.best "12.2.4 Best viable function")) selects
the best conversion function to perform the conversion[.](#6.sentence-3)

[*Example [5](#example-5)*: struct X {operator int();};

struct Y : X {operator char();};

void f(Y& a) {if (a) { // error: ambiguous between X​::​operator int() and Y​::​operator char()}} â *end example*]

â *end note*]

[7](#7)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L2709)

Conversion functions can be virtual[.](#7.sentence-1)

[8](#8)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L2713)

A conversion function template shall not have a
deduced return type ([[dcl.spec.auto]](dcl.spec.auto "9.2.9.7 Placeholder type specifiers"))[.](#8.sentence-1)

[*Example [6](#example-6)*: struct S {operator auto() const { return 10; } // OKtemplate<class T>operator auto() const { return 1.2; } // error: conversion function template}; â *end example*]
