Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Out of declaration template definitions for template method in template class

Tags:

c++

templates

Does anyone know the syntax for an out-of-declaration template method in a template class.

for instance:

template<class TYPE>
class thing
{
public :
  void do_very_little();

  template<class INNER_TYPE>
  INNER_TYPE do_stuff();
};

The first method is defined:

template<class TYPE>
void thing<TYPE>::do_very_little()
{
}

How do I do the second one, "do_stuff"?

like image 853
Simon Parker Avatar asked May 20 '09 05:05

Simon Parker


People also ask

What is template What is the need of template declare a template class?

Templates in c++ is defined as a blueprint or formula for creating a generic class or a function. Generic Programming is an approach to programming where generic types are used as parameters in algorithms to work for a variety of data types.In C++, a template is a straightforward yet effective tool.

How do you define a template function outside class?

It's quite common to separate a template into 2 files, one being a traditional header, and the second being the implementation, as with non-templated functions and their implementation. The only difference is that you need to #include the template implementation file as well as the header when you want to use it.

How do you declare a template function in C++?

Defining a Function TemplateA function template starts with the keyword template followed by template parameter(s) inside <> which is followed by the function definition. In the above code, T is a template argument that accepts different data types ( int , float , etc.), and typename is a keyword.

What are templates How are templates used to define classes and functions?

A template allows us to create a family of classes or family of functions to handle different data types. Template classes and functions eliminate the code duplication of different data types and thus makes the development easier and faster. Multiple parameters can be used in both class and function template.


1 Answers

template<class TYPE>
template<class INNER_TYPE>
INNER_TYPE thing<TYPE>::do_stuff()
{
    return INNER_TYPE();
}

Try this.

like image 191
CMinus Avatar answered Sep 28 '22 08:09

CMinus