Having some trouble with a template with a variable number of parameters

I'm trying to write a print function to just print all its parameters. My current code (I know #include is the one stated in the standard, but my compiler supports #import and I like it)

1
2
3
4
5
6
7
8
9
10
11
12
13
#import <iostream>

template<typename arg1c,typename...argc>
void print(arg1c arg1,argc...args)
{
    std::cout << arg1;
    print(args...);
}

int main()
{
    print("Hello, ","world!\n");
}


Basically, how the print function is meant to work is it prints its first argument and then takes print(allotherargs). It eventually gets to print(one_arg) and prints that. I'm trying to figure out why it isn't working. My first suspicion was that a typename... needs to have at least one typename, but adding

1
2
3
4
5
template<typename T>
void print(T a)
{
    std::cout << a;
}


didn't work. Now my suspicion is that it doesn't know how to deal with print();, but I have no idea how to add a template specialization to cope with this. Ideas?
Last edited on
Try the following

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

template<typename arg1c>
void print(arg1c arg1)
{
    std::cout << arg1;
}

template<typename arg1c,typename...argc>
void print(arg1c arg1,argc...args)
{
    std::cout << arg1;
    print(args...);
}

int main()
{
   print("Hello, ","world!\n");
   
   return 0;
}
Works! Thanks! But how is it different from my code with the function I mentioned I added added?

EDIT: Nvmd, figured it out, I managed to declare the other print afterwards. Fail.
Last edited on
I do know where you added it. It has to be before the first function.
I meant before the call of the first function.
Last edited on
MinGW G++ 4.7.1, working fine, with an #import warning.
Copy-pasted your code together with the print() overload...

I can't help but comment. Isn't it retarded how you need to overload a template function?
¿would you be able to understand otherwise?
1
2
3
void print(){
   std::cout << std::endl;
}
Topic archived. No new replies allowed.