Page 2 of 3

Re: Constructor

Posted: May 24th, 2017, 9:57 am
by albinopapa
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.

Re: Constructor

Posted: May 24th, 2017, 10:09 am
by cyboryxmen
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;
}

Re: Constructor

Posted: May 24th, 2017, 12:25 pm
by albinopapa
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;
}

Re: Constructor

Posted: May 24th, 2017, 4:11 pm
by cyboryxmen
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.

Re: Constructor

Posted: May 25th, 2017, 4:14 am
by LuisR14
I totally know about that type cast (i, in fact, knew about different type casts)

Re: Constructor

Posted: November 3rd, 2017, 5:05 pm
by _Java
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?

Re: Constructor

Posted: November 3rd, 2017, 7:34 pm
by albinopapa
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.

Re: Constructor

Posted: November 4th, 2017, 1:31 am
by _Java
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.

Re: Constructor

Posted: November 4th, 2017, 4:12 am
by albinopapa
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.

Re: Constructor

Posted: November 4th, 2017, 4:50 am
by chili
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.