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?
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.
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.
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.
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.
Here's your definition:
template <typename T1, typename T2>
template <typename E>
void SomeClass<T1, T2>::extraTypedMethod(E & e)
{
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With