Hello
I am in a difficulty in finding the size of array.

In following program I am passing the arraysize and the array as a parameters. Instead of doing that can I find the array size in the method?

template <class T, typename ReturnType = float>
class TEnterImp
{
	
public:
	typedef ReturnType (T::*TemplateMethod)(void);
	
	void enter(TemplateMethod *m_TemplateMethods, int arraySize)
	{
		ReturnType r = 
		for (int i =0; i < arraySize; i++ )
		{
			r = (state.*m_TemplateMethods[i])();
		}
	}

};


// This is how i call

float (MenuState::*p_menuStateFunctions[2])() = {NULL};

p_menuStateFunctions[0] = &MenuState::initSetup;
p_menuStateFunctions[1] = &MenuState::createGUI;

TEnterImp<MenuState, float> t;
t.enter(*this, p_menuStateFunctions, 2 );

I have tried following macro
#define length(a) ( sizeof ( a ) / sizeof ( *a ) )

length ( p_menuStateFunctions );


but it gives me 0

help please

Try not to use the raw arrays if you can. But what you want it template deduction.

#include <iostream>
using namespace std;

template<typename T, size_t SIZE>
int arraySize(const T (&array)[SIZE] ){
 return sizeof(array)/sizeof(array[0]);
}

int main(){
 int a[10] = {0};
 cout << arraySize(a) << endl;
}

Edited 5 Years Ago by firstPerson: n/a

Can you use vectors instead of arrays? Then you get a .size() function :)

Thank you
by using a vector i have managed to eliminate that problem.
~solved.

But any idea of what has happened here?

@firstPerson
Thanks for the reply

I am getting following error with the solution

void enter(T &state, CallBackMethod m_CallBackMethod, TemplateMethod *m_TemplateMethods, int metodCount)
	{
		
		int x = arraySize(m_TemplateMethods);
...
}

It's hard to understand

Error	2	error C2784: 'int arraySize(const T (&)[SIZE])' : could not deduce template argument for 'const T (&)[SIZE]' from 'float (__thiscall MenuState::* *)(void)'	i:\static_paths\graphics\projects\fpsgame\ogre1.7\framework\include\tenterimp.h	52
This article has been dead for over six months. Start a new discussion instead.