How to Call a Template Member Function

How to call a template member function?

Just found it:

According to C++'03 Standard 14.2/4:

When the name of a member template specialization appears after . or
-> in a postfix-expression, or after nested-name-specifier in a qualified-id, and the postfix-expression or qualified-id explicitly depends on a template-parameter (14.6.2), the member template name must be prefixed by the keyword template. Otherwise the name is assumed to name a non-template.

Correct code is:

b.template foo<T0>();

Template member function in a non-template class - how to define and call

You need to define the function PriorityMap::find in your header file rather than your cpp file.

The problem is that with template functions, no instantiations are created in a given compilation unit unless the function instantiation is actually used in the said unit. The compilation unit in which you define your function is not the same one in which you actually use it, so in your case no instantiations are actually created. Later when it comes to linking the compilation unit(s) in which the function is used no definition is therefore found, so you get a linker error.

If you want to avoid defining the function in the header file then you can explicitly instantiate it in the cpp file in which it is defined. For example:

template void PriorityMap::find(MyClass);

The downside here is that this cpp file will have to know about all the types that will ever have to be used with PriorityMap::find

Calling member function template of class template

apply() is a member function template, you need to use the keyword template to tell the compiler that it's a template, when calling it with dependent name. Note the difference between FCT<T,N> and MyFct<float,16>, the former depends on template parameters FCT, T and N, while the later doesn't.

Inside a template definition, template can be used to declare that a dependent name is a template.

e.g.

v1 = FCT<T,N>::template apply<Vec1>();
v2 = FCT<T,N>::template apply<Vec2>();

Calling template member functions

Your function printX is not a member template function. Why do you try to call it as a template?

//                          ,--- Not a template, so you must use
// v You must use the function like any other function
void printBar(void) { m_bar.printX(); }

The template keyword is used with member function template of a dependent type. If the function printX was a template and you would like to specify the template argument instead of deduction, then the syntax would just like the example in the question you mentioned.

Template member function specialization of a templated class without specifying the class template parameter

You can use a technique called tag dispatch and replace the template specialisations by function overloads.

template<typename>
struct Tag {};

template <class A>
struct C3
{
void f_impl(Tag<int>) const;
void f_impl(Tag<char>) const;
template<class B>
void f() const {
f_impl(Tag<B>{});
}
};

struct D { static int g(void){ return 999; } };

template <class A>
void C3<A>::f_impl(Tag<int>) const { std::cout<<A::g()+1<<std::endl; }

template <class A>
void C3<A>::f_impl(Tag<char>) const { std::cout<<A::g()+2<<std::endl; }

Then your call site looks exactly as you want:

 C3<D> c3; c3.f<int>();  // expect to see 1000
C3<D> c4; c4.f<char>(); // expect to see 1001

Full example here.

Unable to call a template member function of a template class from another template class

You should add keyword template:

result.template getSubmatrix<3, 3>(0, 0) = Matrix<3, 3, T>();
// ^^^^^^^^

Without it, compiler will think that < is a comparison operator.

PS. In that case, g++ produces a little more understandable error:

error: invalid operands of types <unresolved overloaded function type> and int to binary operator<



Related Topics



Leave a reply



Submit