name-lookup

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

Scope resolution operator being used twice

僤鯓⒐⒋嵵緔 提交于 2019-11-26 21:50:55
问题 namespace libzerocoin { //Commitment class Commitment::Commitment::Commitment(const IntegerGroupParams* p, const Bignum& value): params(p), contents(value) { this->randomness = Bignum::randBignum(params->groupOrder); this->commitmentValue = (params->g.pow_mod(this->contents, params->modulus).mul_mod( params->h.pow_mod(this->randomness, params->modulus), params->modulus)); } I just encountered this function definition on GitHub. I assume that the second and the third "Commitment" refer to the

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,

GCC issue: using a member of a base class that depends on a template argument

坚强是说给别人听的谎言 提交于 2019-11-26 14:40:22
The following code doesn't compile with gcc, but does with Visual Studio: template <typename T> class A { public: T foo; }; template <typename T> class B: public A <T> { public: void bar() { cout << foo << endl; } }; I get the error: test.cpp: In member function ‘void B::bar()’: test.cpp:11: error: ‘foo’ was not declared in this scope But it should be! If I change bar to void bar() { cout << this->foo << endl; } then it does compile, but I don't think I have to do this. Is there something in the official specs of C++ that GCC is following here, or is it just a quirk? This changed in gcc-3.4 .

Why doesn&#39;t ADL find function templates?

試著忘記壹切 提交于 2019-11-26 04:21:32
What part of the C++ specification restricts argument dependent lookup from finding function templates in the set of associated namespaces? In other words, why does the last call in main below fail to compile? namespace ns { struct foo {}; template<int i> void frob(foo const&) {} void non_template(foo const&) {} } int main() { ns::foo f; non_template(f); // This is fine. frob<0>(f); // This is not. } This part explains it: C++ Standard 03 14.8.1.6 : [Note: For simple function names, argument dependent lookup (3.4.2) applies even when the function name is not visible within the scope of the

GCC issue: using a member of a base class that depends on a template argument

倖福魔咒の 提交于 2019-11-26 03:58:53
问题 The following code doesn\'t compile with gcc, but does with Visual Studio: template <typename T> class A { public: T foo; }; template <typename T> class B: public A <T> { public: void bar() { cout << foo << endl; } }; I get the error: test.cpp: In member function ‘void B::bar()’: test.cpp:11: error: ‘foo’ was not declared in this scope But it should be! If I change bar to void bar() { cout << this->foo << endl; } then it does compile, but I don\'t think I have to do this. Is there something

In a templated derived class, why do I need to qualify base class member names with “this->” inside a member function?

做~自己de王妃 提交于 2019-11-26 01:36:15
问题 While I investigate source code of Qt I saw that trolltech guys explicitly use this keyword to access a field on destructor. inline ~QScopedPointer() { T *oldD = this->d; Cleanup::cleanup(oldD); this->d = 0; } So, what\'s the point of this usage? Are there any benefits? Edit: For those who vote for closing this question, I suspect that this usage is for some class inheritance cases A part of QScopedPointer class definition: template <typename T, typename Cleanup = QScopedPointerDeleter<T> >

Propagating &#39;typedef&#39; from based to derived class for &#39;template&#39;

▼魔方 西西 提交于 2019-11-26 01:28:42
I'm trying to define base class, which contains typedef's only. template<typename T> class A { public: typedef std::vector<T> Vec_t; }; template<typename T> class B : public A<T> { private: Vec_t v; // fails - Vec_t is not recognized }; Why in B I receive an error that Vec_t is not recognized and I need to write it explicitly? typename A<T>::Vec_t v; Kirill V. Lyadvinsky I believe that this question is duplicate, but I cannot find it now. C++ Standard says that you should fully qualify name according to 14.6.2/3: In the definition of a class template or a member of a class template, if a base

Why doesn&#39;t ADL find function templates?

微笑、不失礼 提交于 2019-11-26 01:15:44
问题 What part of the C++ specification restricts argument dependent lookup from finding function templates in the set of associated namespaces? In other words, why does the last call in main below fail to compile? namespace ns { struct foo {}; template<int i> void frob(foo const&) {} void non_template(foo const&) {} } int main() { ns::foo f; non_template(f); // This is fine. frob<0>(f); // This is not. } 回答1: This part explains it: C++ Standard 03 14.8.1.6 : [Note: For simple function names,

Propagating &#39;typedef&#39; from based to derived class for &#39;template&#39;

走远了吗. 提交于 2019-11-26 01:07:35
问题 I\'m trying to define base class, which contains typedef\'s only. template<typename T> class A { public: typedef std::vector<T> Vec_t; }; template<typename T> class B : public A<T> { private: Vec_t v; // fails - Vec_t is not recognized }; Why in B I receive an error that Vec_t is not recognized and I need to write it explicitly? typename A<T>::Vec_t v; 回答1: I believe that this question is duplicate, but I cannot find it now. C++ Standard says that you should fully qualify name according to 14