[英]Return initializer list instead of vector in std::function
Edit: It is not duplicated of the linked question (which is mine also).
编辑:它不是重复的链接问题(也是我的)。 Here all the return types are
std::vector
.这里所有的返回类型都是
std::vector
。 I do not want to return aninitializer-list
.我不想返回
initializer-list
。 I want to fill the returnedstd::vector
byinitializer-list
directly我想直接用
initializer-list
填充返回的std::vector
Let us take those four cases: 让我们来看看这四个案例:
1) 1)
//Acceptable
std::vector<int> foo(){
return std::vector<int>{1};
}
2) 2)
//Acceptable
std::vector<int> foo(){
return {1};
}
3) 3)
//Acceptable
std::function<std::vector<int>()> foo=[](){
return std::vector<int>{1};
};
4) 4)
//NOT Acceptable
std::function<std::vector<int>()> foo=[](){
return {1};
};
Why 4 is not acceptable since 2 is acceptable? 为什么4不可接受,因为2是可以接受的? what is the different between them?
他们之间有什么不同? Moreover, the most strange thing that this is acceptable:
而且,这是可以接受的最奇怪的事情:
//Acceptable
auto bar=[]()->std::vector<int>{
return {1};
};
What is wrong with std::function
and initializer-list
? std::function
和initializer-list
什么问题?
auto bar=[]()->std::vector<int>{
specifies the return type of the lambda bar
to be std::vector<int>
. auto bar=[]()->std::vector<int>{
指定lambda bar
的返回类型为std::vector<int>
。
std::function<std::vector<int>()> foo=[](){
does not specify the return type of foo
, because you first deduce the return type of the lambda, then assign it. std::function<std::vector<int>()> foo=[](){
不指定foo
的返回类型,因为你首先推导出lambda的返回类型,然后分配它。
C++ does not take into account what you may assign the lambda to when deciding on a type, it sees return {1}, which is an std::initializer_list<int>
, which is incompatible with a std::function<std::vector<int>>
. C ++没有考虑在决定类型时你可以为lambda分配什么,它会看到return {1},这是一个
std::initializer_list<int>
,它与std::function<std::vector<int>>
不兼容std::function<std::vector<int>>
。
This variation compiles: 此变体编译:
std::function<std::vector<int>()> foo=[]()->std::vector<int>{
return {1};
};
This is identical to your case 4, except for the explicit return type in the lambda expression. 除了lambda表达式中的显式返回类型之外,这与您的情况4相同。 This shows that the type of the
std::function<>
declaration does not propagate into the parsing of the lambda expression; 这表明
std::function<>
声明的类型不会传播到lambda表达式的解析中; the lambda is parsed independently of the surrounding expression. lambda的解析独立于周围的表达式。
I'm not sure whether this is a feature of the C++ language standard or of the limitations of real-world compilers (I tested with g++ -std=c++11
), since I'm not too much of a language lawyer. 我不确定这是否是C ++语言标准的特性或现实世界编译器的限制(我用
g++ -std=c++11
测试过),因为我不是一个语言律师。
The return type of the lambda in (4) is auto
, and not std::vector
as in (2) and as in your last sample, where you are still using a lambda, but forced the return type. (4)中lambda的返回类型是
auto
,而不是std::vector
如(2)和上一个示例中的那样,你仍在使用lambda,但强制返回类型。
The deduction of auto
in return {1}
leads to std::initializer_list<int>()
, that's different from std::vector<int>()
the std::function
expects. 在
return {1}
推导auto
会导致std::initializer_list<int>()
,这与std::function
期望的std::vector<int>()
不同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.