不能衰减到指针的不完整数组的类型

时间:2016-11-24 09:59:09

标签: c++ templates c++03 incomplete-type

以下代码可以由gcc编译和执行:

#include <stdio.h>

template <typename T>
T&  f1(T & t) {
  printf("T\n");
  return t;
} 

extern int a[];

int main()
{
    f1(a);


    return 0;
}

但是在VC ++ 2010编译时产生了错误:

1>------ Build started: Project: cpptest, Configuration: Debug Win32 ------
1>  proc1.cpp
1>  cpptest.cpp
1>c:\work\cpptest\cpptest\cpptest.cpp(18): error C2664: 'f1' : cannot convert parameter 1 from 'int []' to 'int (&)[1]'
1>  Generating Code...
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

主要问题是函数模板参数是一个引用类型,这意味着数组参数不能衰减为指针类型。根据C ++ 03,标准行为应该是什么?如果允许,在函数f1()及其返回类型中,T的类型(现在是不完整的数组类型)应该是什么?

编辑:在C ++中,按引用传递的数组不会根据此link衰减到指针。这是真的吗?

0 个答案:

没有答案