【问题标题】:How can I explicitly write an array reference return type? [duplicate]如何显式编写数组引用返回类型? [复制]
【发布时间】:2014-09-29 21:33:35
【问题描述】:

我想知道如何明确返回数组引用的函数的返回类型。

typedef int const (Three_Const_Ints)[3];

Three_Const_Ints const & foo ()
{
    static int const values[] = { 0, 1, 2 };
    return values;
}

int const (&)[3] bar ()   // Does not compile. What is the proper syntax?
{
    static int const values[] = { 0, 1, 2 };
    return values;
}

是的,我可以使用std::array,但我很想知道这个语法。

【问题讨论】:

    标签: c++ syntax


    【解决方案1】:

    要返回对数组的引用,请使用以下语法:

    int const (& bar())[3];
    

    【讨论】:

      【解决方案2】:

      试试下面的

      int const (& bar() )[3]
      {
          static int const values[] = { 0, 1, 2 };
          return values;
      }
      

      或者

      typedef const int Three_Const_Ints[3];
      
      Three_Const_Ints & f()
      {
          static Three_Const_Ints values = { 0, 1, 2 };
          return values;
      }
      

      【讨论】:

        【解决方案3】:

        是这样的:

        int const (&bar())[3];
        

        【讨论】:

          猜你喜欢
          • 2013-03-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-09-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-05-21
          相关资源
          最近更新 更多