1

Stroustrup C++ 4th Ed Page 748 has an example of dependent names with regards to member templates. Does anyone know what the dependent name is in this example? Is it all based on the template parameter Alloc?

Thanks!

class Pool { 
public:
    template<typename T>
    T∗ get();
    template<typename T>
    void release(T∗); 
};

template<typename Alloc>
void f(Alloc& all)
{
    int∗ p1 = all.get<int>();
    int∗ p2 = all.template get<int>();
}

void user(Pool& pool)
{ 
    f(pool);
}

Also Stroustrup states that "Compared to the use of typename to explicitly state that a name is assumed to name a type, the use of template to explicitly state that a name is assumed to name a template is rare.". Curious, why would this example be so rare?

notaorb
  • 1,404
  • 3
  • 13
  • 2
    `get` is a dependent name. Before `Alloc` is known, the compiler can't even tell whether `all.get` refers to a template, and whether the ` – Igor Tandetnik Jul 04 '20 at 22:42
  • It's rare because you're usually not invoking a template method on a template type parameter. – cdhowie Jul 04 '20 at 22:44
  • @IgorTandetnik are there not multiple dependent names? His example on p747 claims that. – notaorb Jul 04 '20 at 22:49
  • I only see one in the example shown. I don't have the book. – Igor Tandetnik Jul 04 '20 at 23:16
  • @igortandetnik If I define a non-template member function `int* get() {return &t;}` and call it as above `int* p2 = all.get();` it works. Isn't get still a dependent name here? – notaorb Jul 05 '20 at 00:17
  • @IgorTandetnik What the name `get` a dependent on? – notaorb Jul 05 '20 at 00:31
  • `get` is dependent on the template parameter. Maybe it's a member function; maybe it's a data member of class type with overloaded `operator()`; no way to tell until the template is instantiated with a particular argument. And yes, in `all.get()` it's still a dependent name - it's just that, when it's not followed by ` – Igor Tandetnik Jul 05 '20 at 01:13
  • @IgorTandetnik. That helped clear up some significant confusion on my side! thanks! – notaorb Jul 05 '20 at 01:21

0 Answers0