I heard C++ templates wont generate errors until they are used. Is it true ? Can someone explain me how they work ?
4 Answers
Templates follow two phase compilation model.
struct X{
private:
void f(){}
};
template<class T> void f(T t){
int; // gives error in phase 1 (even if f(x) call is commented in main)
t.f(); // gives error only when instantiated with T = X, as x.f() is private, in phase 2
}
int main(){
X x;
f(x);
}
-
Actually, the standard mandates that template follow the two phases compilation model, but some compilers (most notably VS) do not and thus only give warning / errors during the second phase (unless it's just so bad that the parser chokes). Commented Aug 19, 2010 at 6:55
-
@Mattieu and chubsdad: Is there a particular place in the standard where this is described? Commented Aug 19, 2010 at 7:27
-
Refer Section 10.3 of C++ Templates by David Vandervoorde. Section 14.6 actually talks indirectly about this concept. e.g. "[Note: if a template is instantiated, errors will be diagnosed according to the other rules in this Standard. Exactly when these errors are iagnosed is a quality of implementation issue. ]"– ChubsdadCommented Aug 19, 2010 at 7:39
-
1@Matthieu and chubsdad: Found it. 14.6/7: "If no valid specialization can be generated for a template definition, and that template is not instantiated, the template definition is ill-formed, no diagnostic required." So it is not a compliance issue at all. The standard strongly suggests such checks and declares that such support improves "quality of implementation," but it is also clear about diagnostics on non-instantiated templates being not required. The paragraph is not changing for C++0x. I found some articles with a contrary view, but poor referencing; I would call those FUD. Commented Aug 19, 2010 at 7:52
-
@chubsdad: if the standard doesn't talk about something directly, it's not in there. Commented Aug 19, 2010 at 7:54
They generate compiler errors when they are compiled. They are compiled separately for each actual parameter passed as the template argument(s) (this is unlike Java Generics), e.g., if I have:
template <typename T> class foo { ... }
and
int main() {
foo<char> c;
foo<int> i ;
}
the template foo
gets compiled twice, once for chars, once for ints.
If you never (directly or indirectly) instantiated or used template foo
, it wouldn't be compiled and you'd not see any compiler errors.
Once compiled, they're just "normal" C++ code, and like any code, can generate runtime errors.
-
2This still doesn't allow for syntactically invalid code which will raise errors in the first pass. Commented Aug 19, 2010 at 6:19
-
1Part of the confusion probably comes from the fact, that VC++ skips the first pass, so you can put any gibberish in the templates as long as they are not instantiated. Commented Aug 19, 2010 at 6:38
-
-
3
-
@tpdi: see comments on above answer; standard describes such checks but clearly states they do not affect compliance. Commented Aug 19, 2010 at 7:57
From here,
From the point of view of the compiler, templates are not normal functions or classes. They are compiled on demand, meaning that the code of a template function is not compiled until an instantiation with specific template arguments is required. At that moment, when an instantiation is required, the compiler generates a function specifically for those arguments from the template.
Hope it helps..
-
In general terms of a template class, this means that the compiler will generate a different type for each type of template class declared. Hence
vector<int>
is not the same asvector<double>
. If you don't declare anyvector
types, then there are no types generated.– AnthonyCommented Aug 19, 2010 at 5:53 -
@ Duracell, Yeah I agree and that's what meant by, compiler generates a function specifically for those arguments...– liaKCommented Aug 19, 2010 at 5:57
Conceptually, at the highest level
template <Type value, class Y, ...> ...fn-or-class...
may be usefully compared to
#define FN_OR_CLASS(VALUE, TYPE_Y, ...) \ ...fn-or-class...
Both basically wait until called/instantiated then substitute the specified types and values to produce tailored code with full compile-time optimisation for those values. But, templates differ from #defines in that they're proper compile-stage constructs that can be enclosed in namespaces, must satisfy the lexer, and not all of a class template is generated when the first instantiation is seen - rather, functions are generated on an as-needed basis.
When the compiler first encounters a template, it does a rough check that the template's content could make sense for some hypothetical instantiation. Later, when it encounters a specific instantiation, then for class templates only the functions that are used are further checked to make sure they can be compiled with the specific parameters in use. This does mean that a class template can appear - for some limited usage - to support instantiation with specific parameters, but if you start using some other functions in the template API then suddenly you can find that it can't be compiled with that presumed-suitable parameter... can force you to redesign your usage rather late in the day. That is one of the reasons that C++0x had planned to introduce Concepts: they elegantly allow templates to check that parameters meet all the template's expectations - if they allow any instantiation, then the user can assume that the full API of the template can be used.
template <class T> struct X { void f() { } void g() { T::whatever(); } // only error if g() called w/o T::whatever }; int main() { X<int> x; x.f(); // x.g(); // would cause an error as int::whatever() doesn't exist... }
The SFINAE (substitution failure is not an error) technique can then allow the compiler to select between multiple nearly-matching functions based on the actual instantiating template parameters. This can be used to implement basic compile-time introspection, such as "does this class have a member function fn(int)?".
-
"… it does a rough check that the template's content…" - which may result in errors. Commented Aug 19, 2010 at 7:26