Constructor

The Partridge Family were neither partridges nor a family. Discuss.
albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Constructor

Post by albinopapa » May 24th, 2017, 9:57 am

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

User avatar
cyboryxmen
Posts: 190
Joined: November 14th, 2014, 2:03 am

Re: Constructor

Post by cyboryxmen » May 24th, 2017, 10:09 am

Which is why it's more useful with metaprogramming than normal programming. You can just let the compiler deduce the size.

Code: Select all

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

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Constructor

Post by albinopapa » May 24th, 2017, 12:25 pm

Huh, cool, thanks for taking the time.

This would be the same thing though, right?

Code: Select all

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

User avatar
cyboryxmen
Posts: 190
Joined: November 14th, 2014, 2:03 am

Re: Constructor

Post by cyboryxmen » May 24th, 2017, 4:11 pm

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

User avatar
LuisR14
Posts: 1248
Joined: May 23rd, 2013, 3:52 pm
Location: USA
Contact:

Re: Constructor

Post by LuisR14 » May 25th, 2017, 4:14 am

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.
[url=irc://irc.freenode.net/#pchili]irc://irc.freenode.net/#pchili[/url] [url=irc://luisr14.no-ip.org/#pchili]alt[/url] -- join up if ever want real-time help or to just chat :mrgreen: --

User avatar
_Java
Posts: 4
Joined: February 16th, 2017, 12:45 am
Location: Indiana, United States

Re: Constructor

Post by _Java » November 3rd, 2017, 5:05 pm

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: Select all

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

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Constructor

Post by albinopapa » November 3rd, 2017, 7:34 pm

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: Select all

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: Select all

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

User avatar
_Java
Posts: 4
Joined: February 16th, 2017, 12:45 am
Location: Indiana, United States

Re: Constructor

Post by _Java » November 4th, 2017, 1:31 am

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

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Constructor

Post by albinopapa » November 4th, 2017, 4:12 am

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

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Constructor

Post by chili » November 4th, 2017, 4:50 am

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

Post Reply