dependent-name

Compiler error when using integer as template parameter

走远了吗. 提交于 2019-12-07 02:53:23
问题 What is wrong with the following piece of code? template<typename X> struct A { template<int N> int foo() const { return N; } }; template<typename X> struct B { int bar(const A<X>& v) { return v.foo<13>(); } }; #include <iostream> using std::cout; using std::endl; int main() { A<double> a; B<double> b; cout << b.bar(a) << endl; return 0; } Inside the function B::bar the compiler complains: error: invalid operands of types ‘’ and ‘int’ to binary ‘operator<’ If A is not a template, everything

Accessing types from dependent base classes

吃可爱长大的小学妹 提交于 2019-12-06 19:14:06
问题 Does anyone know why using-declarations don't seem to work for importing type names from dependent base classes? They work for member variables and functions, but at least in GCC 4.3, they seem to be ignored for types. template <class T> struct Base { typedef T value_type; }; template <class T> struct Derived : Base<T> { // Version 1: error on conforming compilers value_type get(); // Version 2: OK, but unwieldy for repeated references typename Base<T>::value_type get(); // Version 3: OK, but

Compiler error when using integer as template parameter

给你一囗甜甜゛ 提交于 2019-12-05 07:05:51
What is wrong with the following piece of code? template<typename X> struct A { template<int N> int foo() const { return N; } }; template<typename X> struct B { int bar(const A<X>& v) { return v.foo<13>(); } }; #include <iostream> using std::cout; using std::endl; int main() { A<double> a; B<double> b; cout << b.bar(a) << endl; return 0; } Inside the function B::bar the compiler complains: error: invalid operands of types ‘’ and ‘int’ to binary ‘operator<’ If A is not a template, everything compiles fine. Change return v.foo<13>(); to return v.template foo<13>(); because foo is a dependent

Accessing types from dependent base classes

断了今生、忘了曾经 提交于 2019-12-05 00:56:58
Does anyone know why using-declarations don't seem to work for importing type names from dependent base classes? They work for member variables and functions, but at least in GCC 4.3, they seem to be ignored for types. template <class T> struct Base { typedef T value_type; }; template <class T> struct Derived : Base<T> { // Version 1: error on conforming compilers value_type get(); // Version 2: OK, but unwieldy for repeated references typename Base<T>::value_type get(); // Version 3: OK, but unwieldy for many types or deep inheritance typedef typename Base<T>::value_type value_type; value

Conditional operator's return type and two-phase lookup

这一生的挚爱 提交于 2019-12-03 14:24:59
问题 Consider the following snippet: struct Base { }; struct Derived : Base { }; void f(Base &) { std::cout << "f(Base&)\n"; } template <class T = int> void g() { Derived d; f(T{} ? d : d); // 1 } void f(Derived &) { std::cout << "f(Derived&)\n"; } int main() { g(); } In this case, I reckon that the function call to f at // 1 should be looked up in phase one, since its argument's type is unambigously Derived& , and thus be resolved to f(Base&) which is the only one in scope. Clang 3.8.0 agrees

Conditional operator's return type and two-phase lookup

廉价感情. 提交于 2019-12-03 05:15:40
Consider the following snippet: struct Base { }; struct Derived : Base { }; void f(Base &) { std::cout << "f(Base&)\n"; } template <class T = int> void g() { Derived d; f(T{} ? d : d); // 1 } void f(Derived &) { std::cout << "f(Derived&)\n"; } int main() { g(); } In this case, I reckon that the function call to f at // 1 should be looked up in phase one, since its argument's type is unambigously Derived& , and thus be resolved to f(Base&) which is the only one in scope. Clang 3.8.0 agrees with me , but GCC 6.1.0 doesn't , and defers the lookup of f until phase two, where f(Derived&) is picked

Visual C++ Compiler allows dependent-name as a type without “typename”?

若如初见. 提交于 2019-12-01 17:38:09
问题 Today one of my friends told me that the following code compiles well on his Visual Studio 2008: #include <vector> struct A { static int const const_iterator = 100; }; int i; template <typename T> void PrintAll(const T & obj) { T::const_iterator *i; } int main() { std::vector<int> v; A a; PrintAll(a); PrintAll(v); return 0; } I usually use g++, and it always refuse to pass the second PrintAll() call. As I know, for this problem, g++ is doing the standard way translating a template. So, is my

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

女生的网名这么多〃 提交于 2019-11-29 01:29:53
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 on a template-parameter, the base class scope is not examined during unqualified name lookup either at

calling template function of template base class [duplicate]

隐身守侯 提交于 2019-11-28 23:25:58
Possible Duplicate: Where and why do I have to put the “template” and “typename” keywords? Here's the code: template<typename T> class base { public: virtual ~base(); template<typename F> void foo() { std::cout << "base::foo<F>()" << std::endl; } }; template<typename T> class derived : public base<T> { public: void bar() { this->foo<int>(); // Compile error } }; And, when running: derived<bool> d; d.bar(); I get the following errors: error: expected primary-expression before ‘int’ error: expected ‘;’ before ‘int’ I'm aware of non-dependent names and 2-phase look-ups . But, when the function

calling template function of template base class [duplicate]

本小妞迷上赌 提交于 2019-11-27 14:42:19
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Where and why do I have to put the “template” and “typename” keywords? Here's the code: template<typename T> class base { public: virtual ~base(); template<typename F> void foo() { std::cout << "base::foo<F>()" << std::endl; } }; template<typename T> class derived : public base<T> { public: void bar() { this->foo<int>(); // Compile error } }; And, when running: derived<bool> d; d.bar(); I get the following