I (vaguely) know that a template is not created if not used. For example, the following code will compile even if T::type does not make sense when T = int .
template<typename T> struct A { void f() { using type = typename T::type; } }; A<int> a;
It compiles because f() not used, so no instance is created - thus, the reality of T::type remains uncontrollable. It doesn't matter if any other member function g() calls f() .
template<typename T> struct A { void f() { using type = typename T::type; } void g() { f(); }
It also compiles fines . But here I am aware of the nebula in my understanding of the definition of "use." I'm asking:
- Is
f() still unused? How exactly?
I can clearly see that it is used inside g() . But then I thought that since g() not used, f() also not used, in terms of instantiating. This seems reasonable enough. still.
However, if I add the virtual to g() , it does not compile:
template<typename T> struct A { void f() { using type = typename T::type; } virtual void g() { f(); }
The result is a compilation error , because now it is trying to create an instance of f() . I do not understand this behavior.
Can anyone explain this? Especially the influence of the virtual on the definition of "use" of a class template member.
c ++ instantiation c ++ 11 virtual templates
Nawaz Oct 28 '13 at 8:32 2013-10-28 08:32
source share