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 keywordtemplate
. 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>
andint
to binaryoperator<
Related Topics
Stl Map Should Use Find() or [N] Identifier to Find Element in Map
How to Use Clang++ with -Std=C++11 -Weverything -Werror
Is There a Reason Why Not to Use Link-Time Optimization (Lto)
How to Include Correctly -Wl,-Rpath,$Origin Linker Argument in a Makefile
C++11 Scope Exit Guard, a Good Idea
What Is the Size of a Pointer? What Exactly Does It Depend On
C++ Unordered_Map Fail When Used with a Vector as Key
Cmake Simple Config File Example
Initialize Std::Array with a Range (Pair of Iterators)
Is Array Name a Constant Pointer in C++
Getting a Bunch of Crosses Initialization Error
Should I Return an Rvalue Reference (By Std::Move'Ing)
Vector: Initialization or Reserve
How to Execute a Command and Get Return Code Stdout and Stderr of Command in C++
Efficiency of Postincrement V.S. Preincrement in C++
How to Create a Thread-Safe Singleton Pattern in Windows
Qt 5.5 Embed External Application into Qwidget
C++ Linux Double Destruction of Static Variable. Linking Symbols Overlap