Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define template function within template class in *.inl file

I write template declaration in *.hpp file and their "definition" in *.inl file linked from *.hpp

just like this:

//*.hpp
template <typename T1, typename T2>
class SomeClass
{
public:
    void someMethod();
};

//*.inl
template <typename T1, typename T2>
void SomeClass<T1, T2>::someMethod()
{
}

but how to write extra templated method inside template class in *.inl file?

//*.hpp
template <typename T1, typename T2>
class SomeClass
{
public:
    void someMethod();

    template <typename E>
    void extraTypedMethod(E & e);
};

//*.inl
template <typename T1, typename T2>
void SomeClass<T1, T2>::someMethod()
{
}

//how can I here define extraTypedmethod?
like image 744
relaxxx Avatar asked Oct 05 '11 09:10

relaxxx


People also ask

How do you declare a template function?

To instantiate a template function explicitly, follow the template keyword by a declaration (not definition) for the function, with the function identifier followed by the template arguments. template float twice<float>( float original ); Template arguments may be omitted when the compiler can infer them.

Can I define template function in CPP?

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.

Why template function only defined header files?

A template is not like a function which can be compiled into byte code. It is just a pattern to generate such a function. If you put a template on its own into a *. cpp file, there is nothing to compile.


1 Answers

Here's your definition:

template <typename T1, typename T2>
template <typename E>
void SomeClass<T1, T2>::extraTypedMethod(E & e)
{
}
like image 166
Nicola Musatti Avatar answered Oct 18 '22 04:10

Nicola Musatti