Jump to content

  • Log In with Google      Sign In   
  • Create Account


Varidic templates as additional parameters


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 Juliean   GDNet+   -  Reputation: 2363

Like
0Likes
Like

Posted 04 May 2013 - 04:19 PM

Hello,

 

I wonder if it was at all possible to use the c++11 feature of variadic template to act as an optional paramters, aside from some fixed ones? I'm trying to abstract the process of creating a new game state for my state system. All of my main game states (main menu, editor, game) have four fixed objects they need, plus some optional parameters:

 

        class BaseState :
            public IState
        {
        public:
            BaseState(gfx::IGfx3D& gfx3D, ecs::EntityManager& entityManager, ecs::SystemManager& systemManager, ecs::MessageManager& messageManager);
            ~BaseState(void);

        protected:

            template<typename S, typename... Args>
            void NewState(Args&&... args);

            gfx::IGfx3D* m_pGfx3D;
            ecs::EntityManager* m_pEntities;
            ecs::SystemManager* m_pSystems;
            ecs::MessageManager* m_pMessages;

            IState* m_pCurrentState;
        };

        template<typename S, typename... Args>
        void BaseState::NewState(Args&&... args)
        {
            m_pCurrentState = new S(*m_pGfx3D, *m_pEntities, *m_pSystems, *m_pMessages, args...);
        }

Not talking about how I could probably pack the four variables into one to make the constructor less ugly... calling it that way:

 

void MainMenuState::OnNewGame(void)
{
	NewState<GameState>();
}

results in the following error:

 

1>C:\Acclimate Engine\Repo\Editor\Core/BaseState.h(44): error C2661: 'GameState::GameState' : no overloaded function takes 5 arguments
1>          MainMenuState.cpp(26) : see reference to function template instantiation 'void acl::core::BaseState::NewState<GameState,>(void)' being compiled

Obviously, it still takes the variadic template as a fifth parameter, even though normally varidic templates "extinct" if I don't submit any in the call. Does somebody know how to solve that?

 



Sponsor:

#2 King Mir   Members   -  Reputation: 1931

Like
1Likes
Like

Posted 04 May 2013 - 06:08 PM

You need to handle the case where there are no variatic arguements seperately. If that's not your problem, post you GameState constructors.

#3 Juliean   GDNet+   -  Reputation: 2363

Like
0Likes
Like

Posted 04 May 2013 - 06:18 PM

Ah, of course, how could I forget that. Its getting late again :/ Did try that though, but it gave me an ambigous symbol error. I forgot to add the one seperate template:

 

		template<typename S>
		void BaseState::NewState(void)
		{
			m_pCurrentState = new S(*m_pGfx3D, *m_pEntities, *m_pSystems, *m_pMessages);
		}

		template<typename S, typename Arg1, typename... Args>
		void BaseState::NewState(Arg1 arg, Args&&... args)
		{
			m_pCurrentState = new S(*m_pGfx3D, *m_pEntities, *m_pSystems, *m_pMessages, arg, args...);
		}


#4 AllEightUp   Moderators   -  Reputation: 4163

Like
0Likes
Like

Posted 04 May 2013 - 06:36 PM

Ah, of course, how could I forget that. Its getting late again :/ Did try that though, but it gave me an ambigous symbol error. I forgot to add the one seperate template:

 

		template<typename S>
		void BaseState::NewState(void)
		{
			m_pCurrentState = new S(*m_pGfx3D, *m_pEntities, *m_pSystems, *m_pMessages);
		}

		template<typename S, typename Arg1, typename... Args>
		void BaseState::NewState(Arg1 arg, Args&&... args)
		{
			m_pCurrentState = new S(*m_pGfx3D, *m_pEntities, *m_pSystems, *m_pMessages, arg, args...);
		}

 

That will be a problem for the compiler because, even though named in the second constructor: "void BaseState::NewState( Arg1 arg )" includes "void arg" as a valid template parameter, so it is still equivalent to "void BaseState::NewState( void )" from the first constructor.  So, both constructors can take the void argument which is ambiguous of course.  In order to correct this, Arg1 needs to be specified as being anything non-void.  So, you may have to write out multiple variations which take arg 1 of different types or give it a fixed type, or remove the first version all together and only use the second version.


Edited by AllEightUp, 04 May 2013 - 08:09 PM.


#5 Juliean   GDNet+   -  Reputation: 2363

Like
0Likes
Like

Posted 05 May 2013 - 07:05 AM

That will be a problem for the compiler because, even though named in the second constructor: "void BaseState::NewState( Arg1 arg )" includes "void arg" as a valid template parameter, so it is still equivalent to "void BaseState::NewState( void )" from the first constructor. So, both constructors can take the void argument which is ambiguous of course. In order to correct this, Arg1 needs to be specified as being anything non-void. So, you may have to write out multiple variations which take arg 1 of different types or give it a fixed type, or remove the first version all together and only use the second version.

 

Did you try this out? Because this is the standard solution I know (which makes it even more emberassing that I didn't remember), since Arg1 cannot resolve to void, thats what makes the difference. Its like if you have a simple template function

 

template<typename A>
void foo(A a)
{
}

You can't just call foo(), can you? At least my solution above compiled for me, and as I said, thats how I did it since I've first heard about varidic templates.



#6 AllEightUp   Moderators   -  Reputation: 4163

Like
0Likes
Like

Posted 05 May 2013 - 10:00 AM

That will be a problem for the compiler because, even though named in the second constructor: "void BaseState::NewState( Arg1 arg )" includes "void arg" as a valid template parameter, so it is still equivalent to "void BaseState::NewState( void )" from the first constructor. So, both constructors can take the void argument which is ambiguous of course. In order to correct this, Arg1 needs to be specified as being anything non-void. So, you may have to write out multiple variations which take arg 1 of different types or give it a fixed type, or remove the first version all together and only use the second version.

 

Did you try this out? Because this is the standard solution I know (which makes it even more emberassing that I didn't remember), since Arg1 cannot resolve to void, thats what makes the difference. Its like if you have a simple template function

 

Unfortunately I missread your message and went looking for a reason it would be failing.. That's what I came up with, though looking at it today I realize the function lookup rules allow the case in terms of finding the closest match.  Oops.. :)

 

template<typename A>
void foo(A a)
{
}

You can't just call foo(), can you? At least my solution above compiled for me, and as I said, thats how I did it since I've first heard about varidic templates.

 

Yup, sorry.  It was late and viewed through beer goggles.. :)



#7 Juliean   GDNet+   -  Reputation: 2363

Like
0Likes
Like

Posted 05 May 2013 - 10:14 AM

Yup, sorry. It was late and viewed through beer goggles.. smile.png

 

Don't worry, if it hadn't been late already, I shouldn't have come up with that question in the first place. We all make mistakes. Anyway, thanks for trying to help out :)






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS