Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

There are no arguments that depend on a template parameter

Tags:

c++

templates

I am trying to do the following:

template <class T> std::ifstream& operator>> (std::ifstream& fin, List<T> l) {     T temp;     l.resize(0);     fin >> ignore(1,'\t');     for(ListIterator<T> i=l.begin();i!=l.end();i++)     {         fin >> ignore(1,'\t') >> temp;         l.push_back(temp);     }     return fin; } 

I have to read all the contents from a file. Each field is separated by '\t' character, so I have to ignore the '\t' characters.

The error log is the following:

/home/ramy/Documents/C++/Prova/Util.h||In function ‘std::ifstream& Util::operator>> (std::ifstream&, Util::List<T>)’:| /home/ramy/Documents/C++/Prova/Util.h|431|error: there are no arguments to ‘ignore’ that  depend on a template parameter, so a declaration of ‘ignore’ must be available| /home/ramy/Documents/C++/Prova/Util.h|431|note: (if you use ‘-fpermissive’, G++ will  accept your code, but allowing the use of an undeclared name is deprecated)| ||=== Build finished: 1 errors, 0 warnings ===| 
like image 232
Ramy Al Zuhouri Avatar asked Mar 30 '12 11:03

Ramy Al Zuhouri


People also ask

How many template arguments are there?

1) A template template parameter with an optional name. 2) A template template parameter with an optional name and a default. 3) A template template parameter pack with an optional name.

What is correct for template parameter?

A template argument for a template template parameter is the name of a class template. When the compiler tries to find a template to match the template template argument, it only considers primary class templates. (A primary template is the template that is being specialized.)

What are template arguments?

In C++ this can be achieved using template parameters. A template parameter is a special kind of parameter that can be used to pass a type as argument: just like regular function parameters can be used to pass values to a function, template parameters allow to pass also types to a function.

CAN default arguments be used with the template?

You cannot give default arguments to the same template parameters in different declarations in the same scope. The compiler will not allow the following example: template<class T = char> class X; template<class T = char> class X { };


2 Answers

For builtin types, argument dependent lookup (ADL) is not performed, therefore, an ignore symbol must be "imported" into the current namespace.

You can, for example, do this; from most preferred to least preferred (i.e. to most intrusive and name polluting):

  • foobar::ignore (...)
  • using foobar::ignore; ignore(...);
  • using namespace foobar; ignore(...);

The error message comes up like this because in templates, you also enter the realm of dependent names and Two Phase Lookup. Names that depend on a template parameter, e.g.

template <typename T> void foo() {     T x;     x.frobnicate(); } 

are looked up in phase 2, which is upon instantiation. Names that do not depend on template parameters, like

class Foo {};  template <typename T> void foo() {     Foo foo;     foo.frobnicate(); } 

must be resolvable in the first phase.

This separation helps template authors to find bugs earlier and to find correct symbols, and it helps making templates more generic. For example, in C# generics, everything must be resolvable, which puts rather stringent limits on their flexibility (because everything that may be used by a generic must be defined). Oppositely, some old C++ compilers resolved in phase 2 only, i.e. at instantiation time, which had some subtle consequences for lookup and error finding.

The C++ 2-phase model combines the best of the eager-model (C#) and the lazy-model (some old C++ compilers).

like image 151
Sebastian Mach Avatar answered Sep 24 '22 18:09

Sebastian Mach


For an easier answer, see https://web.archive.org/web/20130423054841/http://www.agapow.net/programming/cpp/no-arguments-that-depend-on-a-template-parameter

TL;DR: replace ignore() with this->ignore() and your problem will go away.

like image 39
John Hainsworth Avatar answered Sep 24 '22 18:09

John Hainsworth