简体   繁体   English

std::forward 无法转换大括号括起来的初始化列表

[英]std::forward cannot convert brace-enclosed initializer list

Why can't struct screen not initialize the frame struct properly?为什么 struct screen 不能正确初始化框架结构?

What I want is to initialize the screen struct and directly initialize the 2 frame structs as well.我想要的是初始化屏幕结构并直接初始化 2 帧结构。

#include <iostream>
#include <sstream>
#include <cstring>

#define ESC "\033"

struct frame {
  public:
    frame(unsigned int w, unsigned int h) :
      m_w(w),
      m_h(h) {}

  private:
    unsigned int m_w, m_h;
};

struct screen {
  public:
    template<typename ... Args>
      screen(Args && ... args0, Args && ... args1) :
        m_f0(std::forward<Args>(args0)...),
        m_f1(std::forward<Args>(args1)...) {}

  private:
    frame m_f0, m_f1;
};

int main() {
  frame f = {16, 16};

  screen s = {f, {16, 16}};

  return 0;
}

{16, 16} has no type. {16, 16}没有类型。 If used in a context to initialize something with a type, it initializes that thing.如果在上下文中使用某个类型来初始化某物,则它会初始化该事物。

Constructor arguments have a type.构造函数参数有一个类型。 Deduced template constructor arguments get their type from the argument passed.推导出的模板构造函数参数从传递的参数中获取它们的类型。 But {16,16} has no type, so it cannot deduce a type from something that lacks a type.但是{16,16}没有类型,所以它不能从缺少类型的东西推导出类型。

Your second problem is this:你的第二个问题是:

  template<typename ... Args>
  screen(Args && ... args0, Args && ... args1) :
    m_f0(std::forward<Args>(args0)...),
    m_f1(std::forward<Args>(args1)...) {}

C++ won't deduce Args... for you here. C++ 不会在这里为您推断Args... It will only deduce an argument pack if it is the last arguments in the function call, and here Args... is both the last and not the last, so it won't be deduced.如果它是函数调用中的最后一个参数,它只会推导出一个参数包,而这里的Args...既是最后一个又不是最后一个,因此不会被推导出。

Now you can use make from tuple to some extent:现在你可以在某种程度上使用make from tuple

template<class...Args0, class...Args1>
screen( std::tuple<Args0...> args0, std::tuple<Args1...> args1 ):
  m_f0(std::make_from_tuple<frame>(std::move(args0))),
  m_f1(std::make_from_tuple<frame>(std::move(args1)))
{}

which gets you a touch closer (but not close enough).这让你更接近(但不够接近)。 At the call site you'd do:在呼叫站点,您将执行以下操作:

screen s = {std::forward_as_tuple(f), std::forward_as_tuple(16, 16)};

and it should now work.它现在应该可以工作了。

This uses , but make_from_tuple can be implemented as far back as or in C++14 .这使用 ,但make_from_tuple可以早在C++14 中实现

暂无
暂无

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

相关问题 无法转换Brace封闭的初始化列表 - Cannot convert Brace-enclosed initializer list 无法从大括号括起来的初始化列表转换为std元组 - Could not convert from brace-enclosed initializer list to std tuple 无法从大括号括起来的初始化列表转换为std :: vector - Could not convert from brace-enclosed initializer list to std::vector 无法从大括号括起初始化列表转换为struct - Cannot convert to struct from brace-enclosed initializer list 无法转换&#39; <brace-enclosed initializer list> &#39;以&#39;加倍&#39;作为回报 - cannot convert '<brace-enclosed initializer list>' to 'double' in return 无法转换&#39; <brace-enclosed initializer list> 在初始化时从&#39;到&#39;int *&#39; - cannot convert ‘<brace-enclosed initializer list>’ to ‘int*’ in initialization gcc 不是 clang 上的错误:无法从“转换”<brace-enclosed initializer list> ' 到 'std::vector&lt;&gt;</brace-enclosed> - error on gcc not clang: cannot convert from '<brace-enclosed initializer list>' to 'std::vector<> 错误:无法转换&#39; <brace-enclosed initializer list> ()&#39;从&#39; <brace-enclosed initializer list> &#39;到&#39;结构&#39; - Error: could not convert '<brace-enclosed initializer list>()' from '<brace-enclosed initializer list>' to 'struct' 用大括号括起来的初始化程序列表 - brace-enclosed initializer list 无法转换&#39;( <expression error> )”,来自“ <brace-enclosed initializer list> &#39;到std :: unique_ptr <int []> - could not convert '(<expression error>)' from '<brace-enclosed initializer list>' to std::unique_ptr<int []>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM