we32234
we32234

Reputation: 7

Passing variable length argument to function C++

I am trying to create a function that accepts variable number of arguments. But I am getting error in expression decltype(std::initializer_list::size_type) res1=0; as error: expected primary-expression before 'decltype'. The purpose is to declare appropriate variable type that can hold sum of the list (although this declaration will create big enough variable to hold all elements of the list only and not their sum). How can I do this?

Also, how can I make appropriate function return type to return res1 instead of void?

#include <iostream>
#include <initializer_list>

void sum1(std::initializer_list<int> lst1)
{
    decltype(std::initializer_list::size_type) res1=0;
    for(auto v1:lst1)
    {
        res1 += v1;
    }
    std::cout<<" sum = \n"<<res1;
}
int main()
{
    sum1({1,2,3,4,5,6,7,8,9,10});
    //auto b = sum1({1,2,3,4,5,6,7,8,9,10});
    return 0;
}

Upvotes: 1

Views: 50

Answers (2)

Matthew James Briggs
Matthew James Briggs

Reputation: 2265

size_type is not needed for anything in your function. The initializer_list has type int, therefore res1 should be an int and your return type should be int. If you really want to derive the value type from the initializer_list then do so like this:

#include <iostream>
#include <initializer_list>

auto sum1(std::initializer_list<int> lst1)
{
    typename std::initializer_list<int>::value_type res1 = 0;
    for(auto v1:lst1)
    {
        res1 += v1;
    }

    return res1;
}

int main()
{
    sum1({1,2,3,4,5,6,7,8,9,10});
    auto b = sum1({1,2,3,4,5,6,7,8,9,10});
    std::cout << b << std::endl;
    return 0;
}

If you want the function to be generic, in which case it is necessary to derive the value type, then the template function looks like this.

#include <iostream>
#include <initializer_list>

template<typename T>
auto sum1(std::initializer_list<T> lst1)
{
    typename std::initializer_list<T>::value_type res1 = 0;
    for(auto v1:lst1)
    {
        res1 += v1;
    }

    return res1;
}

int main()
{
    sum1({1.1,2.2,3.3,4.4,5.5,6.6,7.7,8.8,9.9,10.10});
    auto b = sum1({1.1,2.2,3.3,4.4,5.5,6.6,7.7,8.8,9.9,10.10});
    std::cout << b << std::endl;
    return 0;
}

Upvotes: 1

cse
cse

Reputation: 4104

Following is corrected code.

#include <iostream>
#include <initializer_list>

int sum1(std::initializer_list<int> lst1)
{
    int res1=0;
    for(auto v1:lst1)
    {
        res1 += v1;
    }
    return res1;
}
int main()
{
    std::cout<<" sum = "<<sum1({1,2,3,4,5,6,7,8,9,10});
    //auto b = sum1({1,2,3,4,5,6,7,8,9,10});
    return 0;
}

You can see working here.

Note: You can change the type of res0 to long or other type if result is not in the limits of int. Also, Change the return type of function sum1 accordingly.

Upvotes: 0

Related Questions