Jump to content
  • Advertisement
MarcusAseth

How to have tamplates that only takes int[] ?

This topic is 429 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I was doing this experiment, code below

 

#include <iostream>
using namespace std;

template<typename T>
void decay_test(T& arr)
{
	cout << sizeof(arr) << endl;
	cout << "arr has " << sizeof(arr) / sizeof(*arr) << " elements" << endl;
}

int main()
{
	const auto size = 100;
	int arr[size];
	decay_test(arr);

	return 0;
}

output:

Quote

400
arr has 100 elements

Just wanted to see if it was possible to pass and array and it's size into a function with a single argument.

Seems it works, though how one would go about to make it in such a way that the template only takes in arrays of integers? :S

 

Share this post


Link to post
Share on other sites
Advertisement
template <size_t N>
void f(int (&arr)[N])
{
	std::cout << N << std::endl;
}

For taking arbitrary array types, make the int a template parameter as well.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!