Template function inside template class

C++Templates

C++ Problem Overview


I have this code:

template <class T>
class MyClass {
	public:
    	template <class U>
		void foo() {
			U a;
			a.invoke();
		}
};

I want it in this form:

template <class T>
class MyClass {
	public:
    	template <class U>
		void foo();
};

template <class T> /* ????? */
void MyClass<T>::foo() {
    U a;
    a.invoke();
}

How I can to do this? What is the right syntax?

C++ Solutions


Solution 1 - C++

Write this:

template <class T>
template <class U>
void MyClass<T>::foo() { /* ... */ }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMichaelView Question on Stackoverflow
Solution 1 - C++Kerrek SBView Answer on Stackoverflow