explicit conversion operators

F

Fraser Ross

I was looking at the new explicit conversion operators in the standard.
The writing at the start of 12.3.2/2 doesn't make sense. "A conversion
function may be explicit (7.1.2), in which case it is only considered as
a user-defined conversion for direct-initialization (8.5)." That
sentence is ok. "Otherwise, user-defined conversions are not restricted
to use in assignments and initializations." This one contradicts the
other. Initialisation using the = symbol is not assignment.

Is the g() function meant to be an example for 12.3.2/1?

Fraser.
 
F

Fraser Ross

After looking at an older standard I see what it is now saying.
Paragraph 2 makes two different points and would be better to be in two
paragraphs.

Fraser.
 
J

Juha Nieminen

Fraser said:
After looking at an older standard I see what it is now saying.
Paragraph 2 makes two different points and would be better to be in two
paragraphs.

Since you understood that part of the standard, care to also explain
to those who might have got curious about it?
 
F

Fraser Ross

"A conversion function may be explicit (7.1.2), in which case it is only
considered as a user-defined conversion for direct-initialization
(8.5)." I understood that part. Adding explicit disallows all but
direct-initialisation to be done.

"Otherwise, user-defined conversions ": This refers to operators
without the word explicit. "are not restricted to use in assignments
and initializations.": This is not refering to any difference to the
explicit operators as I thought it did. It certainly sounds like it is.
From the last code example we see that the operators can be more widely
used with arithmetic and logic expressions and thats what the sentence
is saying.

Fraser.
 
P

puzzlecracker

Let' have an example.

class Y {
int b;
public:
operator int();
};
operator int() is a conversion function.

Say,

Y y;
int i=y; will invoke the user-defined conversion.


However, the following

class X{
explicit X(int);
}

X x=y;
or X x(y);

is NOT allowed due to explicit keyword. That's another way of
paraphrasing the second part of the statement.
 
D

dean

Let' have an example.

class Y {
int b;
public:
operator int();};

operator int() is a conversion function.

Say,

Y y;
int i=y; will invoke the user-defined conversion.

However, the following

class X{
explicit X(int);

}

X x=y;
or X x(y);

is NOT allowed due to explicit keyword. That's another way of
paraphrasing the second part of the statement.

X x(y) is OK, isn't it?

dean
 
I

Ian Collins

dean said:
X x(y) is OK, isn't it?
Not in this case - X's constructor is private!

But it would be if the constructor were public (and the class ended with
a semicolon).
 
I

Ian Collins

puzzlecracker said:
Let' have an example.

class Y {
int b;
public:
operator int();
};
operator int() is a conversion function.

Say,

Y y;
int i=y; will invoke the user-defined conversion.


However, the following

class X{
explicit X(int);
}

X x=y;
or X x(y);

is NOT allowed due to explicit keyword. That's another way of
paraphrasing the second part of the statement.
Wrong on both counts.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top