Register    Login    Forum    Search    FAQ

Board index » Everything




Post new topic Reply to topic  [ 21 posts ]  Go to page Previous  1, 2, 3  Next
Author Message
 Post subject: Re: Constructor
 Post Posted: May 24th, 2017, 9:57 am 
 

Joined: February 28th, 2013, 3:23 am
Posts: 2828
Location: Oklahoma, United States
Seems error prone if you still have to specify the size again each time you pass it along to a function like that. Would it not be better to just use std::array? Probably not, with the meta stuff, thanks for the info though.

_________________
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com


Top 
 Profile  
Reply with quote  
 Post subject: Re: Constructor
 Post Posted: May 24th, 2017, 10:09 am 
User avatar

Joined: November 14th, 2014, 2:03 am
Posts: 78
Which is why it's more useful with metaprogramming than normal programming. You can just let the compiler deduce the size.

Code:
template<class Type, std::size_t SIZE>
void ProcessArray ( const Type ( &arr ) [ SIZE ] )
{
   for ( std::size_t i = 0; i < SIZE; ++i )
   {
      Process ( arr [ i ] );
   }
}

int main ( )
{
   int arr [ 10 ] { 1, 1, 1, 1, 1, 6, 1, 1, 1, 1 };

   ProcessArray ( arr );

   system ( "pause" );
   return 0;
}

_________________
Zekilk


Top 
 Profile  
Reply with quote  
 Post subject: Re: Constructor
 Post Posted: May 24th, 2017, 12:25 pm 
 

Joined: February 28th, 2013, 3:23 am
Posts: 2828
Location: Oklahoma, United States
Huh, cool, thanks for taking the time.

This would be the same thing though, right?
Code:
template<class Type, std::size_t SIZE>
void ProcessArray( const std::array<Type, SIZE> &arr )
{
   for( const auto &elem : arr )
   {
      Process( elem );
   }
}

int main()
{
   constexpr std::array<int, 10> arr{ 1, 1, 1, 1, 1, 6, 1, 1, 1, 1 };

   ProcessArray( arr );

   system( "pause" );
   return 0;
}

_________________
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com


Top 
 Profile  
Reply with quote  
 Post subject: Re: Constructor
 Post Posted: May 24th, 2017, 4:11 pm 
User avatar

Joined: November 14th, 2014, 2:03 am
Posts: 78
Yeah pretty much. Though that function won't accept actual arrays and vice versa. For the most part, the simpleness of an array is more than enough for me but it is nice to have the ability to overload your function to accept both allowing it the flexibility to work with different code.

_________________
Zekilk


Top 
 Profile  
Reply with quote  
 Post subject: Re: Constructor
 Post Posted: May 25th, 2017, 4:14 am 
User avatar

Joined: May 23rd, 2013, 3:52 pm
Posts: 1236
Location: USA
I totally know about that type cast (i, in fact, knew about different type casts)

_________________
always available, always on, about ~10 years c/c++, java[script], win32/directx api, [x]html/css/php/some asp/sql experience. (all self taught)
Knows English, Spanish and Japanese.
irc://irc.freenode.net/#pchili alt -- join up if ever want real-time help or to just chat :mrgreen: --


Top 
 Profile  
Reply with quote  
 Post subject: Re: Constructor
 Post Posted: November 3rd, 2017, 5:05 pm 
User avatar

Joined: February 16th, 2017, 12:45 am
Posts: 4
Location: Indiana, United States
cyboryxmen wrote:
I guess most people who don't do metaprogramming wouldn't have much knowledge nor use for array types. When you deal with arrays, they would usually decompose into pointers. However, you need to realise that arrays are not just pointers but are their very own type. Instead of decomposing them into pointers, you can get the array itself with the array type Type( &name )[ SIZE ]. Add an & and you'll get the reference type. I use this a lot for metaprogramming with overloading and type specifications.


So like albinopapa mentioned:

Code:
void Func(int( &st )[10]);


does this declare a function that takes an int array of size 10?

_________________
No, I do not write code in Java!

Okay, maybe a little......


Top 
 Profile  
Reply with quote  
 Post subject: Re: Constructor
 Post Posted: November 3rd, 2017, 7:34 pm 
 

Joined: February 28th, 2013, 3:23 am
Posts: 2828
Location: Oklahoma, United States
Yes, that is what it means, it's the same as: int st[10]. Instead of using C style arrays, I would suggest using std::array. It's a lot more readable than ArrType(&arrName)[arrSize].

Code:
template<class ArrType, size_t ArrSize>
void Func( std::array<ArrType, ArrSize> &ArrName )
{
}

vs

template<class ArrType, class ArrSize>
void Func( ArrType(&ArrName)[ArrSize] )
{
}


I guess I'm just more familiar with the container syntax vs the C style array syntax. I'd like to say that the container has the benefit of using the STL algorithms, but in this case, so does the C style arrays.

Code:
Using STL algorithms with C style arrays
auto *beg = &ArrName[0];
auto *end = &ArrName[ArrSize];
std::SomeAlgorithm( beg, end, SomeFunctor );

Using STL algorithms with container
auto beg = ArrName.begin();
auto end = ArrName.end();
std::SomeAlgorithm( beg, end, SomeFunctor );

Both are easy enough I suppose.

_________________
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com


Top 
 Profile  
Reply with quote  
 Post subject: Re: Constructor
 Post Posted: November 4th, 2017, 1:31 am 
User avatar

Joined: February 16th, 2017, 12:45 am
Posts: 4
Location: Indiana, United States
Makes sense now. Seems like overkill kind of when I could pass a pointer, and yea std::array looks nice. I have not looked into it.

_________________
No, I do not write code in Java!

Okay, maybe a little......


Top 
 Profile  
Reply with quote  
 Post subject: Re: Constructor
 Post Posted: November 4th, 2017, 4:12 am 
 

Joined: February 28th, 2013, 3:23 am
Posts: 2828
Location: Oklahoma, United States
Well, the reason to pass the array as cyber does is to maintain the compilers knowledge of size I'm guessing. If you passed a pointer, you'd also have to pass in a size param which may or may not be the size of the block of memory pointed to by the pointer. It has been an instance of bugs from what I've gathered watching talks on the matter. There is a class called span in the Guidelines Support Library called span which allows you to pass in an C style array and be able to use it the same as a STL array.

_________________
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com


Top 
 Profile  
Reply with quote  
 Post subject: Re: Constructor
 Post Posted: November 4th, 2017, 4:50 am 
Site Admin
User avatar

Joined: December 31st, 2011, 4:53 pm
Posts: 3470
Location: Japan
Yeah, passing as a reference to an array also allows stuff like std::begin/end, which means you can use range based for on the array as well. The downside is that you need the exact array size (one function can only process arrays of 1 size). This can be made nicer with templates, but you will still have 1 separate function instantiated for each size of array you call the function with.

_________________
Chili


Top 
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
 
Post new topic Reply to topic  [ 21 posts ]  Go to page Previous  1, 2, 3  Next

Board index » Everything


 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for: