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