Register    Login    Forum    Search    FAQ

Board index » Everything




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

Joined: February 28th, 2013, 3:23 am
Posts: 2708
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: 71
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: 2708
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: 71
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: 1232
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  
Display posts from previous:  Sort by  
 
Post new topic Reply to topic  [ 15 posts ]  Go to page Previous  1, 2

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: