没有依赖于模板参数的参数 [英] There are no arguments that depend on a template parameter

查看:349
本文介绍了没有依赖于模板参数的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试执行以下操作:

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;
}

我必须读取文件中的所有内容。每个字段由'\t'字符分隔,因此我必须忽略'\t'字符

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.

错误日志如下:

/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 ===|


推荐答案

对于内置类型,忽略符号必须是导入的符号,否则不会执行到当前的命名空间。

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 ..)

  • 使用foobar :: ignore; ignore(...);

  • using namespace foobar;忽略(...);

  • 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();
}

在阶段2中查找,不依赖于模板参数的名称,例如

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.

这种分离功能可以帮助模板作者更早地找到错误,并找到正确的符号,这有助于使模板更通用。例如,在C#泛型中,一切都必须是可解析的,这对它们的灵活性提出了相当严格的限制(因为 必须由通用定义)。相反,一些旧的C ++编译器仅在阶段2中解析,即在实例化时间,其对于查找和错误发现具有一些微妙的结果。

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.

C ++ 2阶段模型结合了最好的热切模型(C#)和延迟模型(一些旧的C ++编译器)。

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

这篇关于没有依赖于模板参数的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆