简体   繁体   English

在std :: function中返回初始化列表而不是向量

[英]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 an initializer-list . 我不想返回initializer-list I want to fill the returned std::vector by initializer-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::functioninitializer-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.

相关问题 std::vector 中的初始化列表构造函数 - initializer list constructor in std::vector concat std :: vector和初始化列表 - concat std::vector and initializer list 是否可以从函数返回 std::initializer_list ? - Is it possible to return a std::initializer_list from a function? 尝试使用初始化列表构造`std :: vector`的问题 - Issues trying to construct `std::vector` with initializer list 如何将 std::initializer_list 分配给向量 - how to assign std::initializer_list to a vector 在类的初始化器列表中初始化std :: vector - Initialize std::vector in class's initializer list std::pair 的初始化列表<vector<int> , 双&gt; - Initializer list of std::pair<vector<int>, double> 错误:没有匹配的 function 调用 'std::vector<pet*> ::向量(<brace-enclosed initializer list> )'</brace-enclosed></pet*> - error: no matching function for call to 'std::vector<Pet*>::vector(<brace-enclosed initializer list>)' 用于初始化std :: vector的初始值设定项列表 <std::function<bool(std::string)> &gt;使用g ++ 4.9.0给出错误,但使用Visual Studio 2013可以正常编译 - initializer list to initialize std::vector<std::function<bool(std::string)> > gives error with g++ 4.9.0 but compiles fine with Visual Studio 2013 如何同时接受 std::vector 和 std::initializer_list - How to accept both std::vector and std::initializer_list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM