繁体   English   中英

带 std::array<> 的构造函数委托

[英]Constructor delegation with std::array<>

我有一个 class,它在构造函数中接收一个std::array 对于我的一生,我无法从std::initializer_list中编写一个正确且简单的委托 - 只有我已经注释掉的笨拙的 lambda。 有人可以在没有临时代码的情况下向我展示一种干净明智的方式吗?

#include <array>

class foo
{
  std::array<int,5> t_;
  
public:
  
  foo(): foo{{}}
  { }
  
  foo(std::initializer_list<int> il = {}): foo{ std::array<int,5>(il) }
      // foo { [il]() {
      //   std::array<int,5> tmp;
      //   std::size_t i = 0;
      //   for (auto ile: il)
      //     if (i >= tmp.size()) break;
      //     else tmp[i++]=ile;
      //   return tmp;
      // }()}
  { }
                          
  foo(std::array<int,5> t): t_(t)
  { }
};

这给出:

g++ -std=c++17 -pedantic -Wall -Wextra   -c -o t.o t.cpp
t.cpp: In constructor 'foo::foo(std::initializer_list<int>)':
t.cpp:12:69: error: no matching function for call to 'std::array<int, 5>::array(std::initializer_list<int>&)'
   12 |   foo(std::initializer_list<int> il = {}): foo{ std::array<int,5>(il) }
      |                                                                     ^
In file included from t.cpp:1:
/usr/lib/gcc/x86_64-pc-cygwin/10/include/c++/array:94:12: note: candidate: 'std::array<int, 5>::array()'
   94 |     struct array
      |            ^~~~~
/usr/lib/gcc/x86_64-pc-cygwin/10/include/c++/array:94:12: note:   candidate expects 0 arguments, 1 provided
/usr/lib/gcc/x86_64-pc-cygwin/10/include/c++/array:94:12: note: candidate: 'constexpr std::array<int, 5>::array(const std::array<int, 5>&)'
/usr/lib/gcc/x86_64-pc-cygwin/10/include/c++/array:94:12: note:   no known conversion for argument 1 from 'std::initializer_list<int>' to 'const std::array<int, 5>&'
/usr/lib/gcc/x86_64-pc-cygwin/10/include/c++/array:94:12: note: candidate: 'constexpr std::array<int, 5>::array(std::array<int, 5>&&)'
/usr/lib/gcc/x86_64-pc-cygwin/10/include/c++/array:94:12: note:   no known conversion for argument 1 from 'std::initializer_list<int>' to 'std::array<int, 5>&&'
t.cpp:12:71: error: no matching function for call to 'foo::foo(<brace-enclosed initializer list>)'
   12 |   foo(std::initializer_list<int> il = {}): foo{ std::array<int,5>(il) }
      |                                                                       ^
t.cpp:23:3: note: candidate: 'foo::foo(std::array<int, 5>)'
   23 |   foo(std::array<int,5> t): t_(t)
      |   ^~~
t.cpp:23:3: note:   conversion of argument 1 would be ill-formed:
t.cpp:12:3: note: candidate: 'foo::foo(std::initializer_list<int>)'
   12 |   foo(std::initializer_list<int> il = {}): foo{ std::array<int,5>(il) }
      |   ^~~
t.cpp:12:3: note:   conversion of argument 1 would be ill-formed:
t.cpp:9:3: note: candidate: 'foo::foo()'
    9 |   foo(): foo{{}}
      |   ^~~
t.cpp:9:3: note:   candidate expects 0 arguments, 1 provided
t.cpp:3:7: note: candidate: 'constexpr foo::foo(const foo&)'
    3 | class foo
      |       ^~~
t.cpp:3:7: note:   conversion of argument 1 would be ill-formed:
t.cpp:3:7: note: candidate: 'constexpr foo::foo(foo&&)'
t.cpp:3:7: note:   conversion of argument 1 would be ill-formed:

std::array没有(任何)显式声明的构造函数,因此它没有来自初始化列表的构造函数,因此您不能只将初始化列表转发给数组。 作为解决方法,您可以使用可变参数模板构造函数

template <typename ... Args>
foo(Args && ... args) : foo(std::array<int, 5>({args ...}))

我认为您正在寻找这种语法:

Foo(const int(&values)[5]) 

完整的例子,

#include <array>
#include <algorithm>
#include <iostream>

class Foo
{

public:
    Foo(const int(&values)[5])
    {
        std::copy(std::begin(values), std::end(values), std::begin(m_values));
    }

    const auto& values() const noexcept
    {
        return m_values;
    }

private:
    std::array<int, 5> m_values;
};

int main()
{
    Foo foo({ 1,2,3,4,5 });

    for (const auto& value : foo.values())
    {
        std::cout << value << " ";
    }

    return 0;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM