-
August 19th, 2013, 11:30 AM
#16
Re: how can i convert write() procedure to cout?
![Quote](images/misc/quote_icon.png) Originally Posted by 2kaud
Try something like this. Note that variadic templates in c++11 are not yet implemented in MSVS2010 or 2012.
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?
-
August 19th, 2013, 12:00 PM
#17
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.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
August 19th, 2013, 12:01 PM
#18
Re: how can i convert write() procedure to cout?
![Quote](images/misc/quote_icon.png) 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
-
August 19th, 2013, 12:09 PM
#19
Re: how can i convert write() procedure to cout?
![Quote](images/misc/quote_icon.png) Originally Posted by Cambalinho
i'm using Visual Studio 2010 ![Wink](images/smilies/wink.gif)
Then thats why you get the compile errors. At present neither Visual Studio 2010 nor Visual Studio 2012 supports variadic templates.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
August 19th, 2013, 12:20 PM
#20
Re: how can i convert write() procedure to cout?
![Quote](images/misc/quote_icon.png) 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](images/smilies/cry.gif)
is there another way?
-
August 19th, 2013, 02:15 PM
#21
Re: how can i convert write() procedure to cout?
![Quote](images/misc/quote_icon.png) Originally Posted by Cambalinho
that tutorial is limited, the 1st argument is the master and tell us, indirectly, how many arguments received ![Frown](images/smilies/frown.gif)
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.
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
Last edited by Paul McKenzie; August 19th, 2013 at 02:23 PM.
-
August 19th, 2013, 02:19 PM
#22
Re: how can i convert write() procedure to cout?
![Quote](images/misc/quote_icon.png) 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)
-
August 19th, 2013, 02:27 PM
#23
Re: how can i convert write() procedure to cout?
![Quote](images/misc/quote_icon.png) Originally Posted by Cambalinho
hey the C\C++ is the most powerfull language(i think) ![Wink](images/smilies/wink.gif)
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
-
August 19th, 2013, 04:50 PM
#24
Re: how can i convert write() procedure to cout?
![Quote](images/misc/quote_icon.png) 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![Wink](images/smilies/wink.gif)
that's why i wanted 1 funtion in that way, but seems that i must use overloading functions for the number of arguments![Wink](images/smilies/wink.gif)
thanks for all
-
August 19th, 2013, 09:26 PM
#25
Re: how can i convert write() procedure to cout?
![Quote](images/misc/quote_icon.png) Originally Posted by Cambalinho
i'm build these class for be more easy build my own programming language ![Wink](images/smilies/wink.gif)
that's why i wanted 1 funtion in that way, but seems that i must use overloading functions for the number of arguments ![Wink](images/smilies/wink.gif)
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
-
August 19th, 2013, 10:26 PM
#26
Re: how can i convert write() procedure to cout?
![Quote](images/misc/quote_icon.png) 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.
Last edited by laserlight; August 19th, 2013 at 10:29 PM.
-
August 20th, 2013, 02:48 AM
#27
Re: how can i convert write() procedure to cout?
![Quote](images/misc/quote_icon.png) 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++ )
-
August 20th, 2013, 10:12 AM
#28
Re: how can i convert write() procedure to cout?
![Quote](images/misc/quote_icon.png) 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](images/misc/quote_icon.png) 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.
-
August 20th, 2013, 10:17 AM
#29
Re: how can i convert write() procedure to cout?
![Quote](images/misc/quote_icon.png) 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?
-
August 20th, 2013, 10:36 AM
#30
Re: how can i convert write() procedure to cout?
![Quote](images/misc/quote_icon.png) 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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|