【发布时间】:2021-08-12 00:54:03
【问题描述】:
这是一个演示程序,其中声明了两个函数,它们都接受对数组的引用。
#include <iostream>
void f( const int ( &a )[5] )
{
std::cout << "void f( const int ( &a )[5] )\n";
}
void f( const int ( &a )[6] )
{
std::cout << "void f( const int ( &a )[6] )\n";
}
int main()
{
f( { 1, 2, 3 } );
return 0;
}
正如所见,第一个函数声明是
void f( const int ( &a )[5] );
第二个函数声明是
void f( const int ( &a )[6] );
而函数调用表达式为
f( { 1, 2, 3 } );
尝试使用 www,ideone.com 上的编译器 C++14 (gcc 8.3) 编译此程序我收到错误
prog.cpp:15:17: error: call of overloaded ‘f(<brace-enclosed initializer list>)’ is ambiguous
f( { 1, 2, 3 } );
^
prog.cpp:3:6: note: candidate: ‘void f(const int (&)[5])’
void f( const int ( &a )[5] )
^
prog.cpp:8:6: note: candidate: ‘void f(const int (&)[6])’
void f( const int ( &a )[6] )
程序不正确吗?
【问题讨论】:
标签: c++ arrays c++14 overloading braced-init-list