“使用自动说明符声明的变量不能出现在其自己的初始化程序中” [英] "a variable declared with an auto specifier cannot appear in its own initializer"

查看:288
本文介绍了“使用自动说明符声明的变量不能出现在其自己的初始化程序中”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


中使用Func_ptr的函数指针声明在
中使用尾随返回类型时,似乎出现了错误。我知道如果将声明和初始化放在同一条语句中,或者只是通过直接指定返回类型来使用标准声明,就可以做到这一点,但是我想了解该语言的局限性,因此有人可以在此错误中解释此错误的含义。下面的代码:


使用自动类型说明符声明的变量不能出现在其
自己的初始化程序中




  #include< utility> 
#include< iostream>

int Func(const std :: pair< int,int> p)
{
std :: cout<< p。第一<< -> << p.second<< std :: endl;
返回1;
}

int main()
{
auto(* Func_ptr)(const std :: pair< int,int>& p)-> int;
//以下错误,带下划线的Func_ptr带有下划线,用自动
//声明的变量//声明符不能出现在其自己的初始化程序中
Func_ptr = Func;
}


解决方案

问题是变量以C ++ 03样式和函数格式声明为C ++ 11方式。使其统一即可使用。

  ///旧方式
int(* Func_ptr1)(const std :: pair< int,int>& p);

// C ++ 11
auto func_ptr2 =& Func;

这里是示例
更有趣的是 Clang能够处理混合物。 / p>

It seems like there's an error when using the trailing return type in the function pointer declaration for Func_ptr. I know I can do it if I put the declaration and initialization in the same statement or simply use the standard declaration by specifying the return type directly, but I want to understand the language's limitations, so can someone please explain what this error means in the code below:

"a variable declared with an auto type specifier cannot appear in its own initializer"

#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;
}

解决方案

Problem is that variable is declared in C++03 style and function format in C++11 way. Make it uniform and it will work.

// the old way
int (*Func_ptr1)(const std::pair<int, int>& p);

// the C++11
auto func_ptr2 = &Func;

Here is example. What is more interesting Clang is able to handle mixture.

这篇关于“使用自动说明符声明的变量不能出现在其自己的初始化程序中”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆