std :: forward无法转换括号括起来的初始化程序列表 [英] std::forward cannot convert brace-enclosed initializer list
问题描述
为什么struct screen无法正确初始化框架结构?
我想要初始化屏幕结构并直接直接初始化2帧结构.
#include< iostream>#include< sstream>#include< cstring>#定义ESC"\ 033"结构框架{上市:frame(unsigned int w,unsigned int h):m_w(w),m_h(h){}私人的:无符号整数m_w,m_h;};结构屏幕{上市:template< typename ... Args>屏幕(Args& ... args0,Args& ... args1):m_f0(std :: forward< Args>(args0)...),m_f1(std :: forward< Args>(args1)...){}私人的:帧m_f0,m_f1;};int main(){帧f = {16,16};屏幕s = {f,{16,16}};返回0;}
{16,16}
没有类型.如果用于上下文中以某种类型初始化某些事物,它将初始化该事物.
构造函数参数具有类型.推导的模板构造函数参数从传递的参数中获取其类型.但是 {16,16}
没有类型,因此它不能从缺少类型的事物中推断出类型.
您的第二个问题是:
模板< typename ... Args>屏幕(Args& ... args0,Args& ... args1):m_f0(std :: forward< Args>(args0)...),m_f1(std :: forward< Args>(args1)...){}
C ++不会在这里为您推断 Args ...
.仅当它是函数调用中的最后一个参数时,它才会推断出一个参数包,而这里的 Args ...
既是最后一个,也不是最后一个,因此不会推论.>
现在,您可以在某种程度上使用从元组中创建:>
template< class ... Args0,class ... Args1>屏幕(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))){}
可以使您更接近(但距离不够近).在呼叫站点上,您可以执行以下操作:
screen s = {std :: forward_as_tuple(f),std :: forward_as_tuple(16,16)};
它现在应该可以工作了.
这使用c ++17 ,但可以追溯到 c ++ 11 或a>.
Why can't struct screen not initialize the frame struct properly?
What I want is to initialize the screen struct and directly initialize the 2 frame structs as well.
#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. 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.
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. 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.
Now you can use make from tuple to some extent:
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 c++17, but make_from_tuple
can be implemented as far back as c++11 or in C++14.
这篇关于std :: forward无法转换括号括起来的初始化程序列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!