小编bb_*_*823的帖子

function`->decltype()`是什么意思

我看过这个函数,但我不知道这里发生了什么:

template <typename Container>
auto MaxElement(Container &c,int num_of_el)->decltype(c[0]){
    int index=0;
    for(int i=1;i<num_of_el;i++)
        if(c[i]>c[index])
            index=i;
    return c[index];
}
Run Code Online (Sandbox Code Playgroud)

这是程序的主要部分:

int main(){
    int a=7;
    vector<decltype(a)> v;
    v.push_back(a);
    a=10;
    v.push_back(5);
    cout<<v[0]<<" "<<v[1]<<endl;

    MaxElement(v,v.size())=10;
    cout<<v[0]<<" "<<v[1]<<endl;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我在理解 MaxElement 函数如何工作方面没有问题,但在理解诸如->decltype(c[0])? 那有什么作用?另外,我们怎样才能做类似的事情MaxElement(v,v.size())=10,这里发生了什么?

c++ decltype trailing-return-type

0
推荐指数
1
解决办法
110
查看次数

标签 统计

c++ ×1

decltype ×1

trailing-return-type ×1