我在 Xcode 4.1 和 Visual Studio 2008 上测试了 c++ 标准 ISO/IEC 14882-03 14.6.1/9 中的代码。两个编译器的输出都与标准的预期结果不同。
代码粘贴在下面。
#include <stdio.h>
#include <iostream>
using namespace std;
void f(char);
template <class T > void g(T t)
{
f(1);
f(T(1));
f(t);
}
void f(int);
void h()
{
g(2);
g('a');
}
void f(int)
{
cout << "f int" << endl;
}
void f(char)
{
cout << "f char" << endl;
}
int main() {
h();
return 0;
}
作为标准的描述。预期的输出应该是
f char
f int
f int
f char
f char
f char
在 Xcode 4.1 上构建并运行代码。输出如下。在构建设置中,我尝试将“Compiler for C/C++/Object-C”更改为 Apple LLVM Compiler 2.1、Gcc 4.2 和 LLVM GCC 4.2。输出是相同的。
f char
f char
f char
f char
f char
f char
在 Microsoft Visual Studio 2008 上构建并运行代码。输出如下。
f int
f int
f int
f int
f char
f char
标准的描述(14.6.1/9)粘贴在下面。
如果名称不依赖于模板参数(如 14.6.2 中所定义),则该名称的声明(或声明集)应在该名称出现在模板定义中的点的范围内;该名称绑定到在该点找到的声明(或多个声明),并且此绑定不受在实例化点可见的声明的影响。[例子:
void f(char);
template<class T> void g(T t)
{
f(1); // f(char)
f(T(1)); // dependent
f(t); // dependent
dd++; // not dependent
}
void f(int);
double dd;
void h()
{
// error: declaration for dd not found
g(2); // will cause one call of f(char) followed // by two calls of f(int)
g(’a’); // will cause three calls of f(char)
——结束示例]
代码对编译器来说格式正确,但输出不同。将此代码移植到不同的平台是非常危险的。
有人知道为什么这些编译器不遵循标准吗?
2011 年 10 月 11 日编辑
根据http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#197,标准中的示例是错误的。我在 Clang 和 Gcc 上测试了下面的代码。
#include <stdio.h>
#include <iostream>
using namespace std;
void f(char);
template <class T > void g(T t)
{
f(1);
f(T(1));
f(t);
}
enum E{ e };
void f(E );
void h()
{
g(e);
g('a');
}
void f(E )
{
cout << "f E" << endl;
}
void f(char)
{
cout << "f char" << endl;
}
int main() {
h();
return 0;
}
输出符合预期。
f char
f E
f E
f char
f char
f char
谢谢,
杰弗里