[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

conversion syntax

REH

10/8/2008 2:30:00 PM

It it permissible to use the constructor style cast with primitives
such as "unsigned long"? One of my compilers accepts this syntax, the
other does not. The failing one chokes on the fact that the type is
not a single identifier or keyword. Which one is correct? For example:

unsigned long x = unsigned long(y);



REH


23 Answers

Juha Nieminen

10/8/2008 2:36:00 PM

0

REH wrote:
> It it permissible to use the constructor style cast with primitives
> such as "unsigned long"? One of my compilers accepts this syntax, the
> other does not. The failing one chokes on the fact that the type is
> not a single identifier or keyword. Which one is correct? For example:
>
> unsigned long x = unsigned long(y);

If I'm not mistaken, if you want to make a C-style cast to a type
which consists of several keywords, you have to enclose them in
parentheses. That is:

unsigned long x = (unsigned long)(y);

Of course the recommended way in C++ is to use a C++ style cast:

unsigned long x = static_cast<unsigned long>(y);

REH

10/8/2008 2:38:00 PM

0

On Oct 8, 10:36 am, Juha Nieminen <nos...@thanks.invalid> wrote:
> REH wrote:
> > It it permissible to use the constructor style cast with primitives
> > such as "unsigned long"? One of my compilers accepts this syntax, the
> > other does not. The failing one chokes on the fact that the type is
> > not a single identifier or keyword. Which one is correct? For example:
>
> > unsigned long x = unsigned long(y);
>
>   If I'm not mistaken, if you want to make a C-style cast to a type
> which consists of several keywords, you have to enclose them in
> parentheses. That is:
>
>     unsigned long x = (unsigned long)(y);
>
>   Of course the recommended way in C++ is to use a C++ style cast:
>
>     unsigned long x = static_cast<unsigned long>(y);

Thanks. I know. I was just wondering if the constructor style was
legal.

Maxim Yegorushkin

10/8/2008 2:43:00 PM

0

On Oct 8, 3:36 pm, Juha Nieminen <nos...@thanks.invalid> wrote:
> REH wrote:
> > It it permissible to use the constructor style cast with primitives
> > such as "unsigned long"? One of my compilers accepts this syntax, the
> > other does not. The failing one chokes on the fact that the type is
> > not a single identifier or keyword. Which one is correct? For example:
>
> > unsigned long x = unsigned long(y);
>
>   If I'm not mistaken, if you want to make a C-style cast to a type
> which consists of several keywords, you have to enclose them in
> parentheses. That is:
>
>     unsigned long x = (unsigned long)(y);
>
>   Of course the recommended way in C++ is to use a C++ style cast:
>
>     unsigned long x = static_cast<unsigned long>(y);

The original question, IMO, involved C++ functional style cast, rather
than C-style cast.

--
Max

Juha Nieminen

10/8/2008 2:46:00 PM

0

REH wrote:
> Thanks. I know. I was just wondering if the constructor style was
> legal.

Well, it is legal as long as your type consists of one single symbol.
For example this will always be legal:

template<typename T>
void foo()
{
T x = T(y);
}

This even if you call it like foo<unsigned long>(). That's because 'T'
is just one symbol.

Rather curiously, this is legal too:

template<typename T>
void foo()
{
T x = (T)y;
}

And this even if T is a class (with a constructor which takes a
parameter of the type of 'y').

pjb

10/8/2008 2:48:00 PM

0

REH <spamjunk@stny.rr.com> writes:

> It it permissible to use the constructor style cast with primitives
> such as "unsigned long"? One of my compilers accepts this syntax, the
> other does not. The failing one chokes on the fact that the type is
> not a single identifier or keyword. Which one is correct? For example:
>
> unsigned long x = unsigned long(y);

Well, C++ syntax is rather complex. It's understandable it's hard to
read, to understand and therefore to implement correctly. I don't
know what is specified there.

Since you observe that's a hard dark corner with diverging
implementation, my advice would be to avoid it. Or to use parentheses.

typedef unsigned long ulong;
ulong x = ulong(y);

or:

unsigned long x = (unsigned long)(y);


--
__Pascal Bourguignon__

REH

10/8/2008 3:05:00 PM

0

On Oct 8, 10:47 am, p...@informatimago.com (Pascal J. Bourguignon)
wrote:
> REH <spamj...@stny.rr.com> writes:
> > It it permissible to use the constructor style cast with primitives
> > such as "unsigned long"? One of my compilers accepts this syntax, the
> > other does not. The failing one chokes on the fact that the type is
> > not a single identifier or keyword. Which one is correct? For example:
>
> > unsigned long x = unsigned long(y);
>
> Well, C++ syntax is rather complex.  It's understandable it's hard to
> read, to understand and therefore to implement correctly.   I don't
> know what is specified there.
>
> Since you observe that's a hard dark corner with diverging
> implementation, my advice would be to avoid it.  Or to use parentheses.
>
>     typedef unsigned long ulong;
>     ulong x = ulong(y);
>
> or:
>
>     unsigned long x = (unsigned long)(y);
>
> --
> __Pascal Bourguignon__

Sigh. Yes, I know that. My question is simply: is the syntax legal or
not?

REH

pjb

10/8/2008 3:30:00 PM

0

Maxim Yegorushkin <maxim.yegorushkin@gmail.com> writes:

> On Oct 8, 3:36 pm, Juha Nieminen <nos...@thanks.invalid> wrote:
>> REH wrote:
>> > It it permissible to use the constructor style cast with primitives
>> > such as "unsigned long"? One of my compilers accepts this syntax, the
>> > other does not. The failing one chokes on the fact that the type is
>> > not a single identifier or keyword. Which one is correct? For example:
>>
>> > unsigned long x = unsigned long(y);
>>
>>   If I'm not mistaken, if you want to make a C-style cast to a type
>> which consists of several keywords, you have to enclose them in
>> parentheses. That is:
>>
>>     unsigned long x = (unsigned long)(y);
>>
>>   Of course the recommended way in C++ is to use a C++ style cast:
>>
>>     unsigned long x = static_cast<unsigned long>(y);
>
> The original question, IMO, involved C++ functional style cast, rather
> than C-style cast.

Which funnily, is not decided when you write:

unsigned long x = (unsigned long)(y);

The compiler could as well decide to parse it as a C cast as a C++
functional style cast. I think it'll be my favorite way to write it,
let the compiler deal with it as it wants :-)

--
__Pascal Bourguignon__

REH

10/8/2008 3:44:00 PM

0

On Oct 8, 11:29 am, p...@informatimago.com (Pascal J. Bourguignon)
wrote:
> Maxim Yegorushkin <maxim.yegorush...@gmail.com> writes:
> > On Oct 8, 3:36 pm, Juha Nieminen <nos...@thanks.invalid> wrote:
> >> REH wrote:
> >> > It it permissible to use the constructor style cast with primitives
> >> > such as "unsigned long"? One of my compilers accepts this syntax, the
> >> > other does not. The failing one chokes on the fact that the type is
> >> > not a single identifier or keyword. Which one is correct? For example:
>
> >> > unsigned long x = unsigned long(y);
>
> >>   If I'm not mistaken, if you want to make a C-style cast to a type
> >> which consists of several keywords, you have to enclose them in
> >> parentheses. That is:
>
> >>     unsigned long x = (unsigned long)(y);
>
> >>   Of course the recommended way in C++ is to use a C++ style cast:
>
> >>     unsigned long x = static_cast<unsigned long>(y);
>
> > The original question, IMO, involved C++ functional style cast, rather
> > than C-style cast.
>
> Which funnily, is not decided when you write:
>
>     unsigned long x = (unsigned long)(y);
>
> The compiler could as well decide to parse it as a C cast as a C++
> functional style cast.   I think it'll be my favorite way to write it,
> let the compiler deal with it as it wants :-)
>

Maxim understands that it was a question about syntax, not semantics.

REH

REH

10/8/2008 4:32:00 PM

0

On Oct 8, 10:29 am, REH <spamj...@stny.rr.com> wrote:
> It it permissible to use the constructor style cast with primitives
> such as "unsigned long"? One of my compilers accepts this syntax, the
> other does not. The failing one chokes on the fact that the type is
> not a single identifier or keyword. Which one is correct? For example:
>
> unsigned long x = unsigned long(y);
>

OK, if I read the sections in the standard defining "simple-type-
specifier" and "postfix-expression" correctly, the syntax is not
valid.

REH

REH

10/8/2008 4:38:00 PM

0

On Oct 8, 11:29 am, p...@informatimago.com (Pascal J. Bourguignon)
wrote:
> Which funnily, is not decided when you write:
>
>     unsigned long x = (unsigned long)(y);
>
> The compiler could as well decide to parse it as a C cast as a C++
> functional style cast.   I think it'll be my favorite way to write it,
> let the compiler deal with it as it wants :-)
>

It don't matter how you write it, when there is a valid conversion
from T to U, these are all equivalent:

U(T);
(U) T;
static_cast<U>(T);


REH