Feb 4, 2017 at 3:26pm UTC
I'm stuck trying to decrease a value while expanding a parameter pack.
This is the best pseudo-code I could come up with.
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
#include <iostream>
#include <typeinfo>
template <typename ... Args>
void DisplayAll(Args ... args)
{
std::cout << "Starting display." << std::endl;
for (const auto & arg : {args...})
{
std::cout << arg << std::endl;
}
std::cout << "Display finished." << std::endl;
}
template <typename T>
std::string CreateArgument(int index)
{
return std::to_string(index);
}
template <typename ... Args>
void RunTest(int StartIndex)
{
DisplayAll(CreateArgument<Args>(StartIndex--)...);
}
int main(int argc, char *argv[])
{
RunTest<std::string, std::string, std::string>(3);
return 0;
}
The code shows 1, 2, 3 and (correctly) warns me about possibly invalid sequence point.
I want it to *reliably* show 3, 2, 1, but I don't know how.
Last edited on Feb 4, 2017 at 3:27pm UTC
Feb 4, 2017 at 4:57pm UTC
Seems like a good place to determine the "index" of arguments would be inside the DisplayAll
function.
Feb 4, 2017 at 6:54pm UTC
DisplayAll cannot change :(
Also, CreateArgument will not return the arg's index.
Feb 5, 2017 at 5:15am UTC
It's unclear what exactly you want. Your statement
Indicates there's some requirements you didn't share.
Is this acceptable? You were very close.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# include <iostream>
template <typename T>
std::string CreateArgument(int index) {
return std::to_string(index);
}
template <typename ... Args>
void RunTest(int start) {
using left_to_right = int [];
std::cout << "Starting display.\n" ;
(void ) left_to_right{0, (std::cout << (CreateArgument<Args>(start--)) << "\n" , 0)...};
std::cout << "Display finished.\n" ;
}
int main () {
RunTest<std::string, std::string, std::string>(3);
}
http://rextester.com/EJXY16073
Last edited on Feb 5, 2017 at 5:59am UTC
Feb 8, 2017 at 8:57pm UTC
Last edited on Feb 8, 2017 at 9:00pm UTC