Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   std::min and 32/64 bits. (http://www.velocityreviews.com/forums/t956785-std-min-and-32-64-bits.html)

Andrea Venturoli 01-22-2013 01:09 PM

std::min and 32/64 bits.
 
Hello.

I've got a style question...
Suppose I have the following code:

std::vector<X> V;
std::min(V.size(),1000u);

Now this will compile on 32b gcc, but not on 64b gcc.
So I can write:

std::vector<X> V;
std::min(V.size(),1000ul);

This will compile on 64b, but not on 32b.



Of course I can write:

std::vector<X> V;
std::min(V.size(),std::vector<X>::size_type(1000)) ;

This will work on both platform.

However, not only I find this utterly ugly, but I feel it might make the
code harder to understand for the newcomer.


Is there any better way to achieve the above?


bye & Thanks
av.

Victor Bazarov 01-22-2013 01:49 PM

Re: std::min and 32/64 bits.
 
On 1/22/2013 8:09 AM, Andrea Venturoli wrote:
> Hello.
>
> I've got a style question...
> Suppose I have the following code:
>
> std::vector<X> V;
> std::min(V.size(),1000u);
>
> Now this will compile on 32b gcc, but not on 64b gcc.


Same with VC++ 2012 Express.

> So I can write:
>
> std::vector<X> V;
> std::min(V.size(),1000ul);
>
> This will compile on 64b, but not on 32b.
>
>
>
> Of course I can write:
>
> std::vector<X> V;
> std::min(V.size(),std::vector<X>::size_type(1000)) ;
>
> This will work on both platform.
>
> However, not only I find this utterly ugly, but I feel it might make the
> code harder to understand for the newcomer.
>
>
> Is there any better way to achieve the above?


Perhaps you could cast the return value of 'size' to what you need:

std::min(int(V.size()), 1000);

Unless, of course, you expect the size to be bigger than 2^31-1, then
cast to 'long long' or some such.

V
--
I do not respond to top-posted replies, please don't ask

Jeff Flinn 01-22-2013 02:19 PM

Re: std::min and 32/64 bits.
 
On 1/22/2013 8:09 AM, Andrea Venturoli wrote:
> Hello.
>
> I've got a style question...
> Suppose I have the following code:
>
> std::vector<X> V;
> std::min(V.size(),1000u);
>
> Now this will compile on 32b gcc, but not on 64b gcc.
> So I can write:
>
> std::vector<X> V;
> std::min(V.size(),1000ul);
>
> This will compile on 64b, but not on 32b.
>
>
>
> Of course I can write:
>
> std::vector<X> V;
> std::min(V.size(),std::vector<X>::size_type(1000)) ;
>
> This will work on both platform.
>
> However, not only I find this utterly ugly, but I feel it might make the
> code harder to understand for the newcomer.


std::min(V.size(),std::size_t(1000)); may be not as ugly?

Jeff



Fred Zwarts \(KVI\) 01-22-2013 03:54 PM

Re: std::min and 32/64 bits.
 
"Andrea Venturoli" wrote in message news:kdm327$d4r$1@speranza.aioe.org...
>
>Hello.
>
>I've got a style question...
>Suppose I have the following code:
>
>std::vector<X> V;
>std::min(V.size(),1000u);
>
>Now this will compile on 32b gcc, but not on 64b gcc.
>So I can write:
>
>std::vector<X> V;
>std::min(V.size(),1000ul);
>
>This will compile on 64b, but not on 32b.
>
>
>
>Of course I can write:
>
>std::vector<X> V;
>std::min(V.size(),std::vector<X>::size_type(1000) );
>
>This will work on both platform.
>
>However, not only I find this utterly ugly, but I feel it might make the
>code harder to understand for the newcomer.
>
>
>Is there any better way to achieve the above?
>
>
> bye & Thanks
>av.


What about std::min<size_t>(V.size(),1000);

Is that still ugly?


Andrea Venturoli 01-24-2013 09:27 AM

Re: std::min and 32/64 bits.
 
On 01/22/13 16:54, Fred Zwarts (KVI) wrote:

> What about std::min<size_t>(V.size(),1000);
>
> Is that still ugly?



Thanks to everyone; I'll answer all in one message.

Jeff: using size_t instead of std::vector<X>::size_type is surely
shorter; however I don't think there is any guarantee they are the same
type.

Same goes for int, Victor.

I like Fred's solution: I hadn't thought about explicitly specifiying
std::min instance.

I'll try std::min<std::vector<X>::size_type>.

bye & Thanks
av.




Victor Bazarov 01-24-2013 01:54 PM

Re: std::min and 32/64 bits.
 
On 1/24/2013 4:27 AM, Andrea Venturoli wrote:
> On 01/22/13 16:54, Fred Zwarts (KVI) wrote:
>
>> What about std::min<size_t>(V.size(),1000);
>>
>> Is that still ugly?

>
>
> Thanks to everyone; I'll answer all in one message.
>
> Jeff: using size_t instead of std::vector<X>::size_type is surely
> shorter; however I don't think there is any guarantee they are the same
> type.
>
> Same goes for int, Victor.


My suggestion of casting to 'int' is based on the fact that '1000' has
that type.

> I like Fred's solution: I hadn't thought about explicitly specifiying
> std::min instance.
>
> I'll try std::min<std::vector<X>::size_type>.


Definitely clearer and easier on the eye! ;-)

V
--
I do not respond to top-posted replies, please don't ask

Andrea Venturoli 01-24-2013 05:53 PM

Re: std::min and 32/64 bits.
 
On 01/24/13 14:54, Victor Bazarov wrote:

> My suggestion of casting to 'int' is based on the fact that '1000' has
> that type.


Right, but 1000 being an int was not my intention, really.



>> I like Fred's solution: I hadn't thought about explicitly specifiying
>> std::min instance.
>>
>> I'll try std::min<std::vector<X>::size_type>.

>
> Definitely clearer and easier on the eye! ;-)


Not really, but less worse, perhaps.



bye & Thanks
av.


88888 Dihedral 01-24-2013 10:06 PM

Re: std::min and 32/64 bits.
 
Luca Risolia於 2013年1月25日星期五UTC+8上午2時20分34秒 寫道:
> On 24/01/2013 10:27, Andrea Venturoli wrote:
>
> > Jeff: using size_t instead of std::vector<X>::size_type is surely

>
> > shorter; however I don't think there is any guarantee they are the same

>
> > type.

>
>
>
> If I remember well, with regard to standard containers using standard
>
> allocators, size_type ends up being a typedef of size_t, so you have
>
> this guarantee in your case.
>
>
>
> Anyway, I would use "std::min<decltype(V.size())>", which is good in the
>
> general case.
>
>
>
>


As far as I know the vector in C++ is an indexed
homogeneous type of objects.

It is not a list of arbitrary types allowed
of objects with a GC built in.

>
> --- news://freenews.netfront.net/ - complaints: news@netfront.net ---



88888 Dihedral 01-25-2013 12:43 AM

Re: std::min and 32/64 bits.
 
Luca Risolia於 2013年1月25日星期五UTC+8上午6時16分15秒 寫道:
> On 24/01/2013 23:06, 88888 Dihedral wrote:
>
> > Luca Risolia於 2013年1月25日星期五UTC+8上午2時20分34秒 寫道:

>
> > As far as I know the vector in C++ is an indexed

>
> > homogeneous type of objects.

>
> >

>
> > It is not a list of arbitrary types allowed

>
> > of objects with a GC built in.

>
>
>
> Sorry, but I don't quite understand what you are talking about. My
>
> answer was about size_t and size_type being the same type in a
>
> particular case, which has nothing to do with the type of objects stored
>
> in a vector or list or with a GC...
>
>
>
>
>
>
>
> --- news://freenews.netfront.net/ - complaints: news@netfront.net ---



A vector of pointers of type void * is allowed in C++.

But it is very inconvenient to set up all different
items of objects in this way in C++.


Balog Pal 01-25-2013 12:49 AM

Re: std::min and 32/64 bits.
 
On 1/24/2013 10:27 AM, Andrea Venturoli wrote:
> I'll try std::min<std::vector<X>::size_type>.


Sure, why use a single word of 6 letters when you can make it into 8
tokens with 25 overall length. O,O

Guess it's time to start the new school of 'Noise-oriented programming'.



All times are GMT. The time now is 09:19 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.