Questions tagged [dependent-name]

A C++ dependent name is a name that depends on a template argument. A non-dependent name does not depend on template arguments. The compiler resolves these two types of names at different points in time.

The issue of dependent and non-dependent names arises in templates. Two instantiations of the same class template may have different members, different types. Names that depend on a template argument are dependent names. Names that don't are non-dependent names. The compiler resolves non-dependent names at the point where a template is defined. Resolution of dependent names is deferred until the point where a template is instantiated.

This can lead to some strange compilation errors:

template <class T> class A
{
protected:
   A(T v) : x(v) {}
   T f() { return x; }
private:
   T x;
};

template <class T> class B : public A<T>
{
public:
   B() : A<T>(42) {}
   T g () { return f(); } // Compilation error
};

The above fails to compile because of the non-dependent usage of f in B::g(). The solution is to turn that non-dependent name into a dependent name. Using a using declaration (using A<T>::f;), using a qualified name (return A<T>::f();) or explicitly using this (return this->f();) are three ways to turn the unqualified, non-dependent f into a dependent name.

63 questions
0
votes
1 answer

method, that accesses a conditional class member, does not compile only when being invoked

i wrote the following class that has a conditional member _s and worksOnlyForString method that accesses the member as std::string. if worksOnlyForString method is not invoked the code compiles even if the member is not std::string. there is a…
Alexander
  • 698
  • 6
  • 14
0
votes
0 answers

friend a dependent typename of a template class

I have 3 classes, Device, Register, and WriteOnlyPolicy, defined as such: Device class Device { public: template inline void write(typename Register::value_type value) { Register::writeRegister(value, this); …
Nicolas Holthaus
  • 7,763
  • 4
  • 42
  • 97
-3
votes
5 answers

Create new files, whose name depends on a variable from the program

I'm trying to write a program that will save "X" number of simple textfiles- however, X is determined by the user once the program has run. I can't seem to find any help on the net as to how to solve the following two things, so any tips will be…
1 2 3 4
5