基于范围的循环&内联函数

时间:2013-06-29 16:35:51

标签: c++ loops c++11 inline

我这里有这段代码。

#include <iostream>
using namespace std;

template <typename T> inline T bigArry(const T data[5])
{
    T level = data[0];
    for(T item : data) // error C2143: syntax error : missing ',' before ':' (1st)
    { //error C2143: syntax error : missing ';' before '{' (3rd)
        if(level<item){ level=item; }
    }
    return  level;
}

int main()
{
    int data[5]={//five variables}
    cout << bigArry(data);//see reference to function template instantiation 'T bigArry<int>(const T [])' being compiled with [ T=int] (2nd)

    return 0;
}

函数bigArry()从5个元素的数组中返回最高值。

问题在于,当我使用基于范围的循环时,它会给出代码中提到的错误。但是当我使用通常的时候,一切都恢复正常。 我的意思是,对我的语法看起来很好,我看不出问题。 我正在使用Visual Studio 2010。

我想问的另一件事是关于内联函数。目前我正在阅读C ++ Primer Plus第6版。我何时知道某个函数何时太大而无法内联?是否有一个标准,代码应该有多短?或者,当我们“认为”它没关系时,我们是否使用内联函数?

2 个答案:

答案 0 :(得分:7)

参数data不是函数模板中的数组。它实际上是一个指针。

此功能

template <typename T> inline T bigArry(const T data[5])

完全相同

template <typename T> inline T bigArry(const T *data)

完全没有区别。

这就是您的代码出现编译错误的原因。

这里有几个修复:

  • 您可以通过引用接受参数,如:

    template <typename T> 
    inline T bigArry(const T (&data)[5]) //NOTE &
    

    那应该有用。但那看起来很麻烦。也许,使用下一个。

  • 或者你可以使用它(如@yzt所建议的):

    template <typename C> 
    inline auto bigArry(C const & data) -> decltype(data[0])
    

    这比上面的(以及下面的一个)更干净,也更灵活。除了传递数组之外,只要data[0]定义明确并且意味着它意味着什么,就可以传递任何容器。

  • 或者,如果您愿意,可以使用std::array<T, 5>作为:

    template <typename T> 
    inline T bigArry(const std::array<T,5> & data) 
    

希望有所帮助。

答案 1 :(得分:6)

这是因为当用作函数参数或作为函数参数传递时,数组类型会衰减为指针。换句话说,您的函数签名等同于

template <typename T> inline T bigArry(const T* data)

基于范围的for循环将data传递给全局std::begin()std::end()函数,以便(分别)获得第一个和一个过去的迭代器 - 容器的最后一个元素。

当然,没有全局std::begin()std::end()函数接受指针,它们也无法有意义地定义:如何确定结束容器只给出了指向其第一个元素的指针?

您可以使用std::array而不是C数组(std::array是C数组周围的零开销包装器),并相应地修改您的调用函数:

template <typename T> inline T bigArry(std::array<T, 5> data)
//                                     ^^^^^^^^^^^^^^^^
{
    T level = data[0];
    for(T item : data)
    {
        if(level<item){ level=item; }
    }
    return  level;
}

int main()
{
    std::array<int, 5> data = {1,2,3,4,5};
//  ^^^^^^^^^^^^^^^^^^^^^^^
    std::cout << bigArry(data);

    return 0;
}

这是live example