Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: instance-creation syntax

30 views
Skip to first unread message
Message has been deleted

Paavo Helde

unread,
Dec 21, 2014, 3:48:51 AM12/21/14
to
r...@zedat.fu-berlin.de (Stefan Ram) wrote in news:syntax-20141221015603
@ram.dialup.fu-berlin.de:

These questions seem to be about the terminology used in the standard;
trying to answer as best as I can, but IANAL.

> #include <iostream>
> #include <ostream>
> #include <string>
>
> int main()
> { ::std::cout << ::std::string( "abc" )<<
> ::std::string( 3, 'c' )<< int() << '\n'; }
>
> How is the syntax of expressions like
>
>::std::string( "abc" )
>::std::string( 3, 'c' )
> int()
>
> called?

Explicit type conversion (5.2.3)

> Is this called a function-style cast?
> (But then, which value is cast in »int()«?).

Here, value is produced by value-initializing an object of type int
(5.2.3). For an int, this means the value is zero-initialized (8.5/8).
So, the answer is 0.

>
> In the first two cases, it seems to create a new object.
> It seems to use direct initialization.

Yes (8.5/16).

> But would one call
> »int()« a case of direct-initialization too?

I think not; this is value-initialization which I think is distinct from
direct-initialization.

> It seems that in the case of the arithmetic type »int«, there
> are syntactically two possible usages: »int()« with empty
> parentheses and »int(x)« with a scalar value »x«. Is this
> correct?

Two possible usages of what?

With int(x), x can be any type which is convertible to int. For example,
an object of a class which declares 'operator int' conversion.

> It seems that in the case of a class, the possible
> arguments are determined by the constructors of the
> class and that - would it not be for the most-vexing
> parse - an expression T( ... ) is possible whenever
> the declaration T x( ... ) with the same arguments »...«
> is possible, and then the value of T( ... ) is the value
> x would have directly after the declaration. Is this correct?

Basically, yes (5.2.3). For a single argument, the standard says the type
conversion expression is equivalent to the corresponding cast expression
(5.4). Maybe this allows a bit more?

>
> And, when one cannot write
>
> T x();
>
> to define x, because this declares a function x, one can
> still write
>
> T x = T();
> . So this would be a use-case for T()?

Yes, sure.

>
> I assume T() is a temporary, that exists only during
> the evaluation of its full-expression, but in the case
> of T x = T(); its value is copied into another object,
> so the value survives. (Actually, in »T x = T();«,
> the »T()« is the full expression, so the value should
> be destroyed at the end of its evaluation:
>
> »Temporary objects are destroyed as the last step in
> evaluating the full-expression (1.9) that (lexically)
> contains the point where they were created.«
>
> This make me wonder how the value of »T()« can be
> copied into x, when it is destroyed as the last step
> in the evaluation of »T()«, which would be just
> /before/ the copying is to take place.)

Obviously the value of the temporary is used for initializing the
declared object, but not sure about the exact terminology.

>
> Even if »::std::string( 3, 'c' )« is called »a
> function-style cast« (I'm not sure), it might make more
> sense to call it an »instance(-creating) expression«,
> because we hardly feel that »3, 'c'« is »cast« to
> a string in this case?

It's called "explicit type conversion".

hth
Paavo
Message has been deleted

Paavo Helde

unread,
Dec 21, 2014, 9:30:11 AM12/21/14
to
r...@zedat.fu-berlin.de (Stefan Ram) wrote in news:int-20141221140608
@ram.dialup.fu-berlin.de:

> Paavo Helde <myfir...@osa.pri.ee> writes:
>>r...@zedat.fu-berlin.de (Stefan Ram) wrote in news:syntax-20141221015603
>>>It seems that in the case of the arithmetic type »int«, there
>>>are syntactically two possible usages: »int()« with empty
>>>parentheses and »int(x)« with a scalar value »x«. Is this
>>>correct?
>>Two possible usages of what?
>
> Two possible usages of »int(«:
> It can be followed either by »)« or by an expression.
>
> (And there are no other possibilities, for example, one
> can never have two arguments as in »int( 2, "a" )«.)

The sequence of tokens 'int' and '(' can appear in other contexts as well,
e.g.

int(*x)() = nullptr;

struct A {
operator int() {return 42;}
};

Öö Tiib

unread,
Dec 21, 2014, 2:08:47 PM12/21/14
to
On Sunday, 21 December 2014 03:27:00 UTC+2, Stefan Ram wrote:
> How is the syntax of expressions like
>
> ::std::string( "abc" )
> ::std::string( 3, 'c' )
> int()
>
> called? Is this called a function-style cast?
> (But then, which value is cast in »int()«?).

I don't think they have some special name. Expression.
More specifically postfix expression. The C++11
'T{}' and 'T{something}' are also from that pack.
[expr.post]:

... some stuff ...
simple-type-specifier ( expression-listopt )
typename-specifier ( expression-listopt )
simple-type-specifier braced-init-list
typename-specifier braced-init-list
... some more stuff ...

Why there must be some special name?
0 new messages