Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > std::min and 32/64 bits.

Reply
Thread Tools

std::min and 32/64 bits.

 
 
Andrea Venturoli
Guest
Posts: n/a
 
      01-22-2013
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.
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      01-22-2013
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
 
Reply With Quote
 
 
 
 
Jeff Flinn
Guest
Posts: n/a
 
      01-22-2013
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


 
Reply With Quote
 
Fred Zwarts \(KVI\)
Guest
Posts: n/a
 
      01-22-2013
"Andrea Venturoli" wrote in message news:kdm327$d4r$(E-Mail Removed)...
>
>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?

 
Reply With Quote
 
Andrea Venturoli
Guest
Posts: n/a
 
      01-24-2013
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.



 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      01-24-2013
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
 
Reply With Quote
 
Andrea Venturoli
Guest
Posts: n/a
 
      01-24-2013
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.

 
Reply With Quote
 
88888 Dihedral
Guest
Posts: n/a
 
      01-24-2013
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: http://www.velocityreviews.com/forums/(E-Mail Removed) ---


 
Reply With Quote
 
88888 Dihedral
Guest
Posts: n/a
 
      01-25-2013
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: (E-Mail Removed) ---



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++.

 
Reply With Quote
 
Balog Pal
Guest
Posts: n/a
 
      01-25-2013
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'.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
if and and vs if and,and titi VHDL 4 03-11-2007 05:23 AM



Advertisments