Infinite parameters

Dec 6, 2016 at 3:36pm
How I can rest of the params?


I did like this, I want to cout every element
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

using namespace std;

void badasscout(int k...)
{
    cout << k;
}

int main()
{
    badasscout(12, 32);
}
Dec 6, 2016 at 3:57pm
See this:

http://www.cplusplus.com/reference/cstdarg/va_start/

But there's no way that you can actually determine the type of the passed 'ellipsis' values.

An alternative would be variadic templates:

http://en.cppreference.com/w/cpp/language/parameter_pack
Dec 6, 2016 at 8:45pm
I did like this

#include <iostream>
#include <math.h>

using namespace std;

template<typename... Args>
void CMD(Args...args)
{
cout << args...;
}

int main()
{
CMD("Test");
}

I dont know how to say to pc to print all args
Dec 7, 2016 at 6:03am
If all your arguments are of the same type you can use an initializer list while if your arguments have different types you have to use a recursive variadic function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <initializer_list>
#include <string>

using namespace std;

template <class T>
void print_func_T( std::initializer_list<T> list )
{
    for( auto elem : list )
    {
        cout << elem << '\t' ;
    }
    cout << '\n';
}
template <typename T>
void print_func_T_var(T t)
{
    cout << t << '\t' ;
}

template<typename T, typename... Args>
void print_func_T_var(T t, Args... args) // recursive variadic function
{
    cout << t << '\t' ;

    print_func_T_var(args...) ;
}

int main()
{
    print_func_T ( { 1, 2, 3, 4, 5,});
    print_func_T ( {'a', 'b', 'c'});
    print_func_T_var (1, 2.3, 'a', "hello");
}

Output
1
2
3
4
5
1       2       3       4       5
a       b       c
1       2.3     a       hello
Process returned 0 (0x0)   execution time : 0.031 s
Press any key to continue.
Dec 7, 2016 at 6:06am
closed account (48T7M4Gy)
https://msdn.microsoft.com/en-us/library/fxhdxye9.aspx
Dec 7, 2016 at 9:02am
I would like to try to make this without any objects.
Dec 7, 2016 at 12:16pm
closed account (48T7M4Gy)
Que?
Dec 10, 2016 at 11:38am
I found the best way of doing this! Thank you very much

template<typename...Args>
void scr_text(Args...args)
{
const int c = sizeof...(args);
const char* test[c] ={args...};
for(int i = 0; i < c; i++)
{
cout << test[i];
}
}
Last edited on Dec 10, 2016 at 11:59am
Dec 10, 2016 at 2:34pm
One risk with your approach is an empty parameter pack which would run up against a size 0 C-style array which the standard disallows though some compliers don't. std::array would allow 0 size though
Dec 10, 2016 at 2:58pm
Yeah, you're right! But here's now, how I can change the value of the parameters?
Dec 10, 2016 at 6:01pm
I thought there was a hint in my previous post ;)

1
2
3
4
5
6
7
8
9
10
template <typename... Args>
void scr_text(Args...args)
{
    const int c = sizeof...(args);
    std::array<Args...,c> test;
    for (auto& elem : test)
    {
        cout << elem << '\n';
    }
}
Dec 11, 2016 at 1:42am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>

namespace cpp17 // requires c++17: uses fold expression (binary left fold)
{
    template < typename... ARGS > 
    std::ostream& print( ARGS&&... args ) { return ( std::cout << ... << args ) ; }
}

namespace cpp11
{
    std::ostream& print() { return std::cout ; }
    
    template < typename FIRST, typename... REST > 
    std::ostream& print( FIRST&& first, REST&&... rest ) 
    { 
        std::cout << first ;
        return print(rest...) ;
    }
}

int main()
{
    {
        using cpp17::print ;
        print() ;
        print( 1234567898765 ) ;
        print( '\t', 1, ' ', 23.4, " hello ", 78.567, '\n' ) ;
    }

    {
        using cpp11::print ;
        print() ;
        print( 1234567898765 ) ;
        print( '\t', 1, ' ', 23.4, " hello ", 78.567, '\n' ) ;
    }
}

http://coliru.stacked-crooked.com/a/dfeca4c3dcea7c38
Topic archived. No new replies allowed.