模板名称查找 [英] Template name lookup
问题描述
我可以使用GCC 3.4.2编译下面的代码,因为函数g是
依赖名称。
模板< class T>
void f1(T t)
{
g(t);
}
void g(int i)
{
}
int main()
{
f1< int>(10);
返回0 ;
}
但是如果我将函数g更改为模板函数,那么事情变得不同了。我无法通过GCC 3.4.2编译,但它可以使用Visual C / C ++ 7工作。
模板< class T>
void f1(T t)
{
g< 0>(t);
}
模板< int I>
void g(int i)
{
}
int main()
{
f1< int>(10);
返回0;
}
GCC的错误信息是:
main.cpp:函数`void f1(T)'' :
main.cpp:23:错误:`g''未声明(首先使用此函数)
main.cpp:23:错误:(报告每个未声明的标识符它出现的每个函数只有一次
。)
main.cpp:在全球范围内:
main.cpp:28:错误: `template< int I> void g(int)''之前使用
声明
为什么?
谢谢,
菲尔
Hi,
I can compile the code below with GCC 3.4.2, because function g is a
"dependent name".
template<class T>
void f1(T t)
{
g(t);
}
void g(int i)
{
}
int main()
{
f1<int>(10);
return 0;
}
But if I change the function g to a template function, things became
different. I cannot pass the compilation with GCC 3.4.2, but it works
fine with Visual C/C++ 7.
template<class T>
void f1(T t)
{
g<0>(t);
}
template<int I>
void g(int i)
{
}
int main()
{
f1<int>(10);
return 0;
}
The error message from GCC is:
main.cpp: In function `void f1(T)'':
main.cpp:23: error: `g'' undeclared (first use this function)
main.cpp:23: error: (Each undeclared identifier is reported only once
for each function it appears in.)
main.cpp: At global scope:
main.cpp:28: error: `template<int I> void g(int)'' used prior to
declaration
Why?
Thanks,
Phil
推荐答案
ph ********** @ yahoo.com 写道:
ph**********@yahoo.com wrote:
但如果我将函数g更改为一个模板功能,事情变得与众不同。我无法通过GCC 3.4.2编译,但它与Visual C / C ++ 7一起工作很好。
模板< class T>
void f1(T t)
{< 0>(t);
}
模板< int I>
void g(int i)
{
}
int main()
{
f1< int>(10);
返回0;
} <来自GCC的错误信息是:
main.cpp:在函数`void f1(T)'':
main.cpp:23:错误:`g''未声明(首先使用这个函数)
main.cpp:23:错误:(每个未声明的标识符只报告一次它出现的每个函数。)
main.cpp:在全局范围:
main.cpp:28:错误:`template< int I>在
声明之前使用的void g(int)''
为什么?
But if I change the function g to a template function, things became
different. I cannot pass the compilation with GCC 3.4.2, but it works
fine with Visual C/C++ 7.
template<class T>
void f1(T t)
{
g<0>(t);
}
template<int I>
void g(int i)
{
}
int main()
{
f1<int>(10);
return 0;
}
The error message from GCC is:
main.cpp: In function `void f1(T)'':
main.cpp:23: error: `g'' undeclared (first use this function)
main.cpp:23: error: (Each undeclared identifier is reported only once
for each function it appears in.)
main.cpp: At global scope:
main.cpp:28: error: `template<int I> void g(int)'' used prior to
declaration
Why?
模板< int I>
void g(int i)
{
}
template< class T>
void f1(T t)
{
g< 0>(t);
}
int main( )
{
f1< int>(10);
返回0;
}
-
Ioannis Vranos
http://www23.brinkster.com/noicys
< ph *** *******@yahoo.com>在消息中写道
news:11 ********************** @ g14g2000cwa.googlegr oups.com ...
<ph**********@yahoo.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
我可以使用GCC 3.4.2编译下面的代码,因为函数g是一个
依赖名称。
模板< ;类T>
void f1(T t)
{
g(t);
}
void g(int i)
{
}
int main()
{
f1< int>(10);
返回0;
}
但如果我将函数g更改为模板函数,事情会变得不同。我无法通过GCC 3.4.2编译,但它与Visual C / C ++ 7一起工作很好。
模板< class T>
void f1(T t)
{< 0>(t);
}
模板< int I>
void g(int i)
{
}
int main()
{
f1< int>(10);
返回0;
} <来自GCC的错误信息是:
main.cpp:在函数`void f1(T)'':
main.cpp:23:错误:`g''未声明(首先使用这个函数)
main.cpp:23:错误:(每个未声明的标识符只报告一次它出现的每个函数。)
main.cpp:在全局范围:
main.cpp:28:错误:`template< int I>在
声明之前使用的void g(int)''
为什么?
Hi,
I can compile the code below with GCC 3.4.2, because function g is a
"dependent name".
template<class T>
void f1(T t)
{
g(t);
}
void g(int i)
{
}
int main()
{
f1<int>(10);
return 0;
}
But if I change the function g to a template function, things became
different. I cannot pass the compilation with GCC 3.4.2, but it works
fine with Visual C/C++ 7.
template<class T>
void f1(T t)
{
g<0>(t);
}
template<int I>
void g(int i)
{
}
int main()
{
f1<int>(10);
return 0;
}
The error message from GCC is:
main.cpp: In function `void f1(T)'':
main.cpp:23: error: `g'' undeclared (first use this function)
main.cpp:23: error: (Each undeclared identifier is reported only once
for each function it appears in.)
main.cpp: At global scope:
main.cpp:28: error: `template<int I> void g(int)'' used prior to
declaration
Why?
第一眼看看你的代码就提出了一个问题使用GCC的模板
实例化模型。问题是连接到
实例化的点和模板被替换的顺序。有关更多
的详细说明,我建议使用例如C ++模板。作者:David
Vandevoorde& Nicolai Josuttis。
无论如何,你可以通过在
f()之前加上g()的定义来轻松自救。
干杯
Chris
A first glance at your code suggested a problem with the GCC′s template
instantiation model. The problem is that connected to the point of
instantiation and the order that templates are substituted. For a more
detailed description I′d suggest for example "C++ templates" by David
Vandevoorde & Nicolai Josuttis.
Anyway, you can help yourself easily by putting the definition of g() before
f().
Cheers
Chris
Chris Theis写道:
Chris Theis wrote:
< ; pH值********** @ yahoo.com>在消息中写道
新闻:11 ********************** @ g14g2000cwa.googlegr oups.com ...
<ph**********@yahoo.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
我可以使用GCC 3.4.2编译下面的代码,因为函数g是
a依赖名称。
模板< class T>
void f1(T t)
g(t);
}
void g(int i)
{
}
int main()
{
f1< int>(10);
返回0;
}
但如果我将函数g更改为模板函数,那么
就变得不同了。我无法通过GCC 3.4.2编译,但是
可以正常使用Visual C / C ++ 7.
模板< class T>
void f1(T t)
{< 0>(t);
}
模板< int I>
void g(int i)
{
}
int main()
{
f1< int>(10);
返回0;
} <来自GCC的错误信息是:
main.cpp:在函数`void f1(T)'':
main.cpp:23:错误:`g''未声明(首先使用这个函数)
main.cpp:23:错误:(对于它出现的每个函数,每个未声明的标识符仅报告
。)
main.cpp:在全局范围:
main.cpp:28:错误:`template< int I>在
声明之前使用的void g(int)''
为什么?
第一眼看你的代码表明GCC的问题
Hi,
I can compile the code below with GCC 3.4.2, because function g is a "dependent name".
template<class T>
void f1(T t)
{
g(t);
}
void g(int i)
{
}
int main()
{
f1<int>(10);
return 0;
}
But if I change the function g to a template function, things became different. I cannot pass the compilation with GCC 3.4.2, but it works fine with Visual C/C++ 7.
template<class T>
void f1(T t)
{
g<0>(t);
}
template<int I>
void g(int i)
{
}
int main()
{
f1<int>(10);
return 0;
}
The error message from GCC is:
main.cpp: In function `void f1(T)'':
main.cpp:23: error: `g'' undeclared (first use this function)
main.cpp:23: error: (Each undeclared identifier is reported only once for each function it appears in.)
main.cpp: At global scope:
main.cpp:28: error: `template<int I> void g(int)'' used prior to
declaration
Why?
A first glance at your code suggested a problem with the GCC′s
>
模板实例化模型。
template instantiation model.
我假设你的意思是Visual C ++而不是GCC在这里?
GNU在拒绝代码时是正确的(Visual C ++没有正确地执行这里的标准
):你不能使用模板参数
在名称之后,除非知道该名称表示
模板。
在C ++的第一轮标准化过程中,它是短暂的
考虑为这个例子添加新语法,但最终
它被认为是不必要的。 (类似于模板g< 0>(t)。)
Daveed
I assume you meant "Visual C++" instead of "GCC" here?
GNU is correct in refusing the code (Visual C++ does not correctly
implement the standard here): You cannot use template arguments
after a name unless it is somehow known that the name denotes a
template.
During the first round of standardization of C++ it was briefly
considered to add new syntax for this very example, but ultimately
it was considered unnecessary. (Something like "template g<0>(t)".)
Daveed
这篇关于模板名称查找的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!