0

I noticed something odd when changing some existing code to take std::initializer_list's as function parameters. This was the original code:

function(type_t<t> parameter)

And this is what I changed only the function declaration to:

function(std::initializer_list<type_t<t>> parameter)

I then compiled my code to get a list of errors of what I need to change because of the modification. I was surprised when there were none. I also ran my program and it behaved just as before. Is proper use and intended behavior of a std::initializer_list?

Edit: Here is the actual code in question. Function definition:

bool Coaxial(const std::initializer_list<Vector2<t>> _l) const
{
    bool result_x = 1;
    bool result_y = 1;

    for(Vector2<t> other : _l)
    {
        if(other.x() != x())
            result_x = 0;
    }

    for(Vector2<t> other : _l)
    {
        if(other.x() != x())
            result_x = 0;
    }

    return (result_x || result_y);
};

And where it is called:

static std::vector<AxisAligned_Rectangle2<t> > Construct(std::vector<Vector2<t> > _set)
{
    std::vector<AxisAligned_Rectangle2<t> > result;

    if(_set.size() == 2)
    {
        if(!_set[0].Coaxial(_set[1]))
        {
            if(std::abs(_set[0].x()) <= std::abs(_set[1].x()) && std::abs(_set[0].y()) <= std::abs(_set[1].y()))
            {
                result.push_back(AxisAligned_Rectangle2<t>(_set[0], _set[1].x() - _set[0].x(), _set[1].y() - _set[0].y()));
            }
            else
            {
                result.push_back(AxisAligned_Rectangle2<t>(_set[1], _set[0].x() - _set[1].x(), _set[0].y() - _set[1].y()));
            }
        }
    }
    else if(_set.size())
    {
        bool valid = 1;
        Vector2<t> origin(_set[0]);
        Vector2<t> opposite(_set[0]);

        for(int i=0; i<_set.size(); ++i)
        {
            if(std::abs(_set[i].x()) <= std::abs(origin.x()) && std::abs(_set[i].y()) <= std::abs(origin.y()))
                origin = _set[i];

            if(std::abs(_set[i].x()) >= std::abs(opposite.x()) && std::abs(_set[i].y()) >= std::abs(opposite.y()))
                opposite = _set[i];
        }

        for(int i=0; i<_set.size(); ++i)
        {
            if(!_set[i].Coaxial(origin) && !_set[i].Coaxial(opposite))
                valid = 0;
        }

        if(valid)
            result.push_back(AxisAligned_Rectangle2<t>(origin, opposite.x() - origin.x(), opposite.y() - origin.y()));
    }

    return result;
};

The code compiles and it works. Note that I'm only inputting a vector, not a vector as a single element of an initializer_list.

4

1 回答 1

0

This:

function(type_t<t> parameter)

And this:

function(std::initializer_list<type_t<t>> parameter)

Are not same. Look at the differences in the following code:

template <class T>
using type_t = T;

template <class T>
void function_1(type_t<T> /*parameter*/)
{

}

template <class T>
void function_2(std::initializer_list<type_t<T>> /*parameter*/)
{

}

int main()
{
    function_1(42);

    // function_2(42); - incorrect

    function_2({ 42 });
    //         ^-  -^
}

You can't just convert type_t to initializer_list. You have to make brace-enclosed list with one element and pass it.

于 2013-06-07T04:23:15.660 回答