使用尾随返回类型时似乎出现错误
Func_ptr 的函数指针声明。我知道如果我将声明和初始化放在同一个语句中或者只是通过直接指定返回类型来使用标准声明就可以做到这一点,但我想了解该语言的限制,所以有人可以解释一下这个错误在代码如下:
“使用 auto 类型说明符声明的变量不能出现在其
自己的初始化程序”
#include <utility>
#include <iostream>
int Func(const std::pair<int, int>& p)
{
std::cout << p.first << "->" << p.second << std::endl;
return 1;
}
int main()
{
auto (*Func_ptr)(const std::pair<int, int>& p) -> int;
//Error below, Func_ptr underlined, "a variable declared with the auto
//specifier cannot appear in its own initializer
Func_ptr = Func;
}
问题是变量以 C++03 风格声明,而函数格式以 C++11 方式声明。让它统一,它就会起作用。
// the old way
int (*Func_ptr1)(const std::pair<int, int>& p);
// the C++11
auto func_ptr2 = &Func;
这是例子 https://wandbox.org/permlink/H4cWAWAAawQmk91i。
什么更有趣Clang 能够处理混合物 https://wandbox.org/permlink/DiwR5TpvPzTDpujG#.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)