dependent-name

What is the rule that allows `this->` to access members of dependent base classes?

陌路散爱 提交于 2019-11-27 02:46:20
问题 As we know, the code below is ill-formed because the member x is in a dependent base class. However, changing x to this->x on the indicated line would fix the error. template <typename T> struct B { int x; }; template <typename T> struct C : B<T> { void f() { int y = x; // Error! } }; int main() { C<int> c; c.f(); } I would like an explanation of how this behaviour is specified in the standard. According to [temp.dep]/3: In the definition of a class or class template, if a base class depends

Two phase name lookup for C++ templates - Why?

大城市里の小女人 提交于 2019-11-26 17:46:54
问题 Why does the C++ standard define two phase lookup for templates? Couldn't non dependent declarations and definitions' lookups be deferred to the instantiation stage as well? 回答1: They could. This is the way most early implementations of templates worked, and is still the way the Microsoft compiler worked. It was felt (in the committee) that this was too error prone; it made it too easy to accidentally hijack a name, with the instantiation in one translation unit picking up a local name,

static_assert dependent on non-type template parameter (different behavior on gcc and clang)

江枫思渺然 提交于 2019-11-26 13:48:26
template <int answer> struct Hitchhiker { static_assert(sizeof(answer) != sizeof(answer), "Invalid answer"); }; template <> struct Hitchhiker<42> {}; While trying to disable general template instantiation with static_assert I discovered that the above code in clang generates the assert error even when the template is not instantiated, while gcc generates the assert error only when instantiating Hitchhiker with a parameter other than 42 . Fiddling around I found that this assert: template <int answer> struct Hitchhiker { static_assert(sizeof(int[answer]) != sizeof(int[answer]), "Invalid answer"

static_assert dependent on non-type template parameter (different behavior on gcc and clang)

社会主义新天地 提交于 2019-11-26 03:44:34
问题 template <int answer> struct Hitchhiker { static_assert(sizeof(answer) != sizeof(answer), \"Invalid answer\"); }; template <> struct Hitchhiker<42> {}; While trying to disable general template instantiation with static_assert I discovered that the above code in clang generates the assert error even when the template is not instantiated, while gcc generates the assert error only when instantiating Hitchhiker with a parameter other than 42 . Fiddling around I found that this assert: template

Where and why do I have to put the “template” and “typename” keywords?

独自空忆成欢 提交于 2019-11-25 22:51:01
问题 In templates, where and why do I have to put typename and template on dependent names? What exactly are dependent names anyway? I have the following code: template <typename T, typename Tail> // Tail will be a UnionNode too. struct UnionNode : public Tail { // ... template<typename U> struct inUnion { // Q: where to add typename/template here? typedef Tail::inUnion<U> dummy; }; template< > struct inUnion<T> { }; }; template <typename T> // For the last node Tn. struct UnionNode<T, void> { //