Infinite parameters

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);
}
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
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
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.
closed account (48T7M4Gy)
https://msdn.microsoft.com/en-us/library/fxhdxye9.aspx
I would like to try to make this without any objects.
closed account (48T7M4Gy)
Que?
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
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
Yeah, you're right! But here's now, how I can change the value of the parameters?
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';
    }
}
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.