Re: how can i convert write() procedure to cout?
Quote:
Originally Posted by
2kaud
Try something like this. Note that variadic templates in c++11 are not yet implemented in MSVS2010 or 2012. :cry:
Code:
#include <iostream>
using namespace std;
void write()
{
cout << " ";
}
template <typename A, typename ...B>
void write(A argHead, B... argTail)
{
cout << argHead << " ";
write(argTail...);
}
int main(void)
{
write(1, 2, "qwerty", 4.45, "five", 6);
return 0;
}
sorry, but i get more than 2 errors:
1 - "error C2143: syntax error : missing ',' before '...' ";
2 - "error C2061: syntax error : identifier 'B'"
i use that library and the namespace. so what is wrong?
Re: how can i convert write() procedure to cout?
What compiler are you using - and does it support c++11 variadic templates? MSVS doesn't yet support these.
Re: how can i convert write() procedure to cout?
Quote:
Originally Posted by
2kaud
What compiler are you using - and does it support c++11 variadic templates? MSVS doesn't yet support these.
i'm using Visual Studio 2010;)
Re: how can i convert write() procedure to cout?
Quote:
Originally Posted by
Cambalinho
i'm using Visual Studio 2010;)
Then thats why you get the compile errors. At present neither Visual Studio 2010 nor Visual Studio 2012 supports variadic templates. :cry:
Re: how can i convert write() procedure to cout?
Quote:
Originally Posted by
2kaud
Then thats why you get the compile errors. At present neither Visual Studio 2010 nor Visual Studio 2012 supports variadic templates. :cry:
is there another way?
Re: how can i convert write() procedure to cout?
Quote:
Originally Posted by
Cambalinho
that tutorial is limited, the 1st argument is the master and tell us, indirectly, how many arguments received:(
It has nothing to do with the tutorial. That is how C++ works.
The receiving function has no idea where your argument list stops when you use a varying number of arguments. So you have to tell that function in some way where or when the last parameter is reached.
There are 3 general ways to specify where/when the last argument is reached.
1) A format list or some sort of string telling the function how to parse the rest of the arguments. (printf() works this way)
2) Some number stating the number of arguments that follow in the list (the examples you have work this way)
3) Some "sentinel" or ending argument, signifying the end of the argument list (functions such as execvp() work this way).
There may be other ways, but these are the first three that come to mind. But in no way can you just write a function that takes a varying number of arguments, not tell the receiving function anything, and then by magic the receiving function knows how many parameters you passed. At least not in C++ before C++ 11.
Quote:
i need use the write() like these:
write(var1, var2, "hello world")
the order i don't care, but i don't use strings or arguments for tell me the number of arguments.
Then use another language, since C++ (again, before C++11) doesn't have this functionality.
Regards,
Paul McKenzie
Re: how can i convert write() procedure to cout?
Quote:
Originally Posted by
Paul McKenzie
It has nothing to do with the tutorial. That is how C++ works.
The receiving function has no idea where your argument list stops when you use a varying number of arguments. So you have to tell that function in some way where or when the last parameter is reached.
There are 3 general ways to specify where/when the last argument is reached.
1) A format list or some sort of string telling the function how to parse the rest of the arguments. (printf() works this way)
2) Some number stating the number of arguments that follow in the list (the examples you have work this way)
3) Some "sentinel" or ending argument, signifying the end of the argument list (functions such as execvp() work this way).
There may be other ways, but these are the first three that come to mind. But in no way can you just write a function that takes a varying number of arguments, not tell the receiving function anything, and then by magic the receiving function knows how many parameters you passed. At least not in C++.
Then use another language, since C++ doesn't have this functionality.
Regards,
Paul McKenzie
hey the C\C++ is the most powerfull language(i think);)
Re: how can i convert write() procedure to cout?
Quote:
Originally Posted by
Cambalinho
hey the C\C++ is the most powerfull language(i think);)
It is powerful, but note that you can't find a function that does what you say you want it to do (at least not with C++ 98 or 03). Why do you think that is?
Regards,
Paul McKenzie
Re: how can i convert write() procedure to cout?
Quote:
Originally Posted by
Paul McKenzie
It is powerful, but note that you can't find a function that does what you say you want it to do (at least not with C++ 98 or 03). Why do you think that is?
Regards,
Paul McKenzie
i'm build these class for be more easy build my own programming language;)
that's why i wanted 1 funtion in that way, but seems that i must use overloading functions for the number of arguments;)
thanks for all
Re: how can i convert write() procedure to cout?
Quote:
Originally Posted by
Cambalinho
i'm build these class for be more easy build my own programming language;)
that's why i wanted 1 funtion in that way, but seems that i must use overloading functions for the number of arguments;)
thanks for all
The functions not only need to be overloaded, but they also need to be template functions.
Code:
template <typename arg1>
void write(arg1 a1)
{
//...
}
template <typename arg1, typename arg2>
void write(arg1 a1, arg2 a2)
{
//...
}
Regards,
Paul McKenzie
Re: how can i convert write() procedure to cout?
Quote:
Originally Posted by Cambalinho
i'm build these class for be more easy build my own programming language
Hmm... I don't see how fiddling with C++ syntax is going to make it easier for you to write a compiler/interpreter for your own programming language. Maybe you want your compiler to output C++ code that is then compiled by a C++ compiler, but between this (your preferred syntax):
Code:
write(var1, var2, "hello world")
and this (standard C++ formatted output for output stream syntax):
Code:
std::cout << var1 << var2 << "hello world"
and this (example syntax of function call chaining):
Code:
write(var1).write(var2).write("hello world")
I doubt you will face a problem with writing your compiler to output them either way, assuming you have the skill to implement for at least one of them. So, if my guess that you intend to use C++ as the output of your compiler is correct, then I suggest just using the standard formatted output.
Re: how can i convert write() procedure to cout?
Quote:
Originally Posted by
laserlight
Hmm... I don't see how fiddling with C++ syntax is going to make it easier for you to write a compiler/interpreter for your own programming language. Maybe you want your compiler to output C++ code that is then compiled by a C++ compiler, but between this (your preferred syntax):
Code:
write(var1, var2, "hello world")
and this (standard C++ formatted output for output stream syntax):
Code:
std::cout << var1 << var2 << "hello world"
and this (example syntax of function call chaining):
Code:
write(var1).write(var2).write("hello world")
I doubt you will face a problem with writing your compiler to output them either way, assuming you have the skill to implement for at least one of them. So, if my guess that you intend to use C++ as the output of your compiler is correct, then I suggest just using the standard formatted output.
but:
1 - how use 'Write' word instead 'cout'?;
2 - can i add it to my class?
(i just convert my new language to C++ and then i call the C++ compiler(it's free... well the Dev C++;))
Re: how can i convert write() procedure to cout?
Quote:
Originally Posted by Cambalinho
i just convert my new language to C++ and then i call the C++ compiler
Yeah, that's what I meant by: "Maybe you want your compiler to output C++ code that is then compiled by a C++ compiler"
Quote:
Originally Posted by Cambalinho
but:
1 - how use 'Write' word instead 'cout'?;
2 - can i add it to my class?
I'm saying: don't do that since it does not gain you anything. Your compiler can just as easily output the "cout version" as it can this "Write version" that you have in mind.
Re: how can i convert write() procedure to cout?
Quote:
Originally Posted by
laserlight
Yeah, that's what I meant by: "Maybe you want your compiler to output C++ code that is then compiled by a C++ compiler"
I'm saying: don't do that since it does not gain you anything. Your compiler can just as easily output the "cout version" as it can this "Write version" that you have in mind.
so instead include the Read()\Write in console class, i make them part of my own language(keywords\functions) is what you are advice me?
Re: how can i convert write() procedure to cout?
Quote:
Originally Posted by Cambalinho
so instead include the Read()\Write in console class, i make them part of my own language(keywords\functions) is what you are advice me?
It depends. I have no clue what is the syntax and semantics of this programming language that you are trying to design and implement. What I do know is that whatever the syntax, you can translate a write to standard output in that language to C++ code involving std::cout, without having to write some C++ function (template, or class) with the C++ syntax that you have in mind. In other words, concentrating on C++ seems like it is taking you off course. Rather, concentrate on the syntax and semantics of your language, and how you will be translating that into C++ without unnecessary frills.