声明对引用类型的引用时出现 GCC 错误
gcc error when declaring reference to reference type
在OpenCV库中有一个
typedef const _InputArray& InputArray;
在我们的代码中,我们有以下函数定义:
void wimshow(const String& winName, InputArray &img) {
编译时,会发生以下错误:
error: cannot declare reference to 'cv::InputArray {aka const class cv::_InputArray&}'
void wimshow(const String& winName, InputArray &img) {
奇怪的是,此错误仅在Cray环境中使用GCC 4.8.1发生。 使用 GCC 4.8.1 在正常的 Linux 环境中编译不会出错。
乍一看,我会说对引用类型的引用无论如何都不是很有意义,但我很好奇什么会导致不同的编译器行为!?
似乎是C++03/C++11的差异。
在 C++11 中,额外的&
(顺便说一下,还有const
(应该被忽略:
[C++11: 8.3.2/6]:
如果 typedef (7.1.3(、类型模板参数 (14.3.1( 或 decltype-specifier (7.1.6.2( 表示对类型T
的引用的类型TR
,则尝试创建类型"对 cvTR
的左值引用"将创建类型"对T
的左值引用",而尝试创建类型"对 cvTR
的右值引用"将创建类型TR
。[ 示例:
int i; typedef int& LRI; typedef int&& RRI; LRI& r1 = i; // r1 has the type int& const LRI& r2 = i; // r2 has the type int& const LRI&& r3 = i; // r3 has the type int& RRI& r4 = i; // r4 has the type int& RRI&& r5 = 5; // r5 has the type int&& decltype(r2)& r6 = i; // r6 has the type int& decltype(r2)&& r7 = i; // r7 has the type int&
—结束示例 ]
这里相关的例子是r1
;虽然typedef int& LRI
并不完全像你的typedef
,但这个例子是等价的,因为下面的段落已经放弃了你的const
:
[C++11: 8.3.2/1]:
[..]除非通过使用 typedef (7.1.3( 或模板类型参数 (14.3( 引入 cv 限定符,否则格式不正确,在这种情况下,将忽略 cv 限定符。[..]
但是,C++03 中不存在[C++11: 8.3.2/6]
措辞!事实上,我们可以用以下示例程序比较两种语言之间的行为:
struct T1 {};
typedef T1& T2;
int main()
{
T1 x;
T2& t = x;
}
- C++03 中的输出:
error: cannot declare reference to 'T2 {aka struct T1&}'
- C++11 中的输出:没有!
(忽略有关未使用变量的警告(
因此,请检查每个平台上的编译标志,以确保在两个平台上使用相同的语言。可能是Cray上的默认值为C++03,但您平台上的默认值为C++11。使用 -std=c++03
/-std=c++11
标志来声明要显式使用哪个标志。
对引用的引用(如const const
(应该被忽略以使元模板编程更容易,所以你在Cray系统上看到的错误是一个错误。
- 在分配和发布递增循环迭代器时C++无限循环(gcc 错误?
- GCC 警告和 gcc 错误消息之间的区别
- 调试模板时出现问题.专门针对 Linux GCC 7、GCC 6、GCC 5、GCC 4.9 错误构建失败:模板参数 1
- typedef名称与可变模板参数名称一致时出现GCC错误
- 条件变量:wait_for.gcc错误
- 不可复制类数据成员的统一初始化导致gcc错误
- 在类中将不完整类型的unique_ptr初始化为 nullptr 时编译 gcc 错误
- GCC-7:错误:无法识别的命令行选项"-M64"
- C++ GCC 错误:"sqrtl"不是"std"的成员
- 在联盟中,不愿命名的未命名结构的成员是错误或GCC错误
- 尾随返回类型中带有 SFINAE 的 GCC 错误
- 与lambda一起使用虚拟继承在初始化列表中捕获此问题的GCC错误
- 通过指向成员的指针对嵌套访问进行编译时评估期间出现 GCC 错误
- 从成员变量访问静态 constexpr 成员,GCC 错误?
- std::seed_seq编译错误(GCC错误?)
- GCC 错误:如果已知函数正常返回,则函数可能是属性"pure"的候选者
- iostream GCC错误,转换为boost::filesystem::iostream for Windows
- GCC错误?链式方法,断开序列点
- 是否存在GCC错误:默认std :: function
- GCC 错误错误的 ELF 类:ELFCLASS64,尽管有 -m64 标志