In a C++ class template deriving from WriterBase<T>, why does an unqualified write_raw() call fail, and when do you need typename and template?
answer
- two moments of lookup
- dependent base is not searched
- this-> makes the name dependent
- typename for types, template for <
basics
~20 sTemplates look up names in two phases. The non-dependent call write_raw() is bound at definition, when the dependent base WriterBase<T> is not searched. Write this->write_raw(); add typename for dependent types and template before dependent member templates.
solid answer
~50 sC++ compiles a template in two phases. At the **definition**, every name that does not depend on a template parameter is looked up and bound. At **instantiation**, names that depend on `T` are resolved. `write_raw("HDR")` has no dependent argument, so it is non-dependent and looked up at definition. Class member lookup searches only non-dependent bases, and `WriterBase<T>` is dependent — it could be specialized for some `T` to lack `write_raw` — so clang reports `use of undeclared identifier 'write_raw'`. `this->write_raw()` or `WriterBase<T>::write_raw()` makes the name dependent and defers it. The parser has a related problem: in `T::size_type n;` it cannot know whether `size_type` is a type, so it assumes a value unless you write `typename`. Likewise `C::template width<4>()` tells it `<` opens a template argument list. C++20 made `typename` optional in contexts where only a type can appear, such as alias declarations; function bodies still need it.
code
cpp · 28 lines#include <cstdio>
#include <string>
#include <vector>
template <class T> struct WriterBase {
std::string out;
void write_raw(const char* s) { out += s; }
};
template <class Container> struct Serializer : WriterBase<Container> {
using value_type = typename Container::value_type; // typename: dependent type
void put_header() {
this->write_raw("HDR"); // this-> defers lookup to instantiation
typename Container::size_type n = 0; // still required in a function body
(void)n;
}
};
struct Codec {
template <int N> static int width() { return N * 8; }
};
template <class C> int field_width() { return C::template width<4>(); }
int main() {
Serializer<std::vector<int>> s;
s.put_header();
std::printf("%s %d\n", s.out.c_str(), field_width<Codec>());
}go deeper
Recall that inside a class template deriving from a base that depends on T, members of that base are reached with this-> and dependent nested types need typename.
Explain dependent versus non-dependent names, why the dependent base is not searched at definition, and the three fixes: this->, qualification, and a using-declaration.
Show the silent-binding hazard when a global of the same name exists, the C++20 type-only contexts that relax typename, and where the template keyword is needed before a member template.
Set a team convention, such as always using this-> for inherited members in templates, so phase-one binding never silently picks a namespace-scope function.
## Two phases of name lookup A C++ template is checked twice: 1. **At the point of definition.** The compiler parses the body and looks up every **non-dependent name** — a name whose meaning cannot change with the template arguments. Errors in them are reported even if the template is never instantiated. 2. **At the point of instantiation.** When `Serializer<std::vector<int>>` is needed, the compiler substitutes the arguments and resolves the **dependent names**: those that involve a template parameter, such as `T::value_type`, `this->member`, or a call with a type-dependent argument. For an unqualified dependent call, argument-dependent lookup also runs from the instantiation context. Clang shows phase one on a template no one calls: ```cpp template <class T> void encode(T v) { checksum(v); flush_buffer(); } ``` ```plaintext error: use of undeclared identifier 'flush_buffer' ``` `checksum(v)` is dependent because `v` has type `T`, so it waits; `flush_buffer()` is not, so it fails immediately. ## The dependent base trap ```cpp template <class T> struct WriterBase { void write_raw(const char* s); }; template <class T> struct Serializer : WriterBase<T> { void put_header() { write_raw("HDR"); } // error }; ``` `write_raw("HDR")` has no dependent argument, so it is a non-dependent name and is looked up at definition. Class member lookup searches only **non-dependent** base classes. `WriterBase<T>` depends on `T`, and the compiler cannot look inside it: someone could later write a full specialization `WriterBase<int>` without a `write_raw`. Clang 21 says `use of undeclared identifier 'write_raw'`. If a global `write_raw` happened to exist, phase one would silently bind to it instead — the more dangerous outcome. Three fixes, all of which make the name dependent: - `this->write_raw("HDR");` — `this` has a dependent type, so lookup is deferred. - `WriterBase<T>::write_raw("HDR");` — qualified with the dependent base; it also suppresses virtual dispatch, so prefer `this->` for virtual members. - `using WriterBase<T>::write_raw;` in the class body — brings the name in once for the whole class. ## The typename disambiguator At definition the compiler does not know what `Container::size_type` is: a type, a static data member, or a function. The language rule is that a dependent qualified name is **assumed not to be a type** unless `typename` says so: ```cpp typename Container::size_type n = 0; // OK Container::size_type n = 0; // error in a function body ``` Clang's message is `missing 'typename' prior to dependent type name 'C::size_type'`. **C++20 relaxed this.** In a **type-only context**, where the grammar allows only a type, `typename` may be omitted. These include: - the type in an alias declaration (`using value_type = Container::value_type;`) - a return type or parameter type of a member function declared in the class - a trailing return type, and the type in a `static_cast` In C++17 mode clang accepts the alias form only with the warning `missing 'typename' prior to dependent type name 'C::value_type' is a C++20 extension`. Inside a function body a declaration such as the one above is not a type-only context, so `typename` is still required in C++23. ## The template disambiguator The same ambiguity hits `<`. In `C::width<4>()`, if `width` is dependent, the parser reads `<` as less-than. Writing `C::template width<4>()` — or `obj.template get<0>()` and `ptr->template get<0>()` — says a template argument list follows. Clang's error without it: `missing 'template' keyword prior to dependent template name 'width'`. ## Summary | Situation | Write | Why | |---|---|---| | Member of a dependent base | `this->f()` or `using Base<T>::f;` | Dependent bases are not searched at definition | | Dependent nested type in a body | `typename T::type x;` | Dependent names are assumed to be values | | Dependent nested type in an alias (C++20) | `using U = T::type;` | Type-only context, `typename` optional | | Dependent member template call | `t.template get<0>()` | Otherwise `<` parses as less-than | ## Why interviewers ask All of these rules follow from one design decision: the compiler checks as much of a template as it can before knowing `T`, so mistakes surface at the template author's desk rather than at every user's. The dependent-base error and the `typename` error are among the first things people hit when moving code into templates, and explaining them correctly shows the candidate knows when the compiler binds a name — not just which keyword silences it.
- Why can an unqualified call inside a template silently call the wrong function instead of failing?A non-dependent unqualified name is bound at the template's definition. If a namespace-scope function with that name is visible there, lookup finds it and never looks into the dependent base, so the template calls the global function even though `WriterBase<T>` has a member of the same name. Writing `this->` makes the call dependent and resolves it in the base at instantiation.
- Is typename needed in template<class T> struct S { using P = typename T::pointer; }; under C++20?No. The type of an alias declaration is a type-only context, so since C++20 `using P = T::pointer;` is accepted; clang in C++17 mode accepts it only as an extension with a warning. Writing `typename` there remains valid, which is why code that must also build as C++17 keeps it.
- The compiler cannot see base-class members of any class template.
- typename is never required since C++20.
- Errors in a template body appear only when the template is instantiated.
- this->write_raw() makes the call virtual.
- template before a member call is only needed for static member functions.
Assumes C++23; C++20 made typename optional in type-only contexts such as alias declarations, while function-body declarations still require it.