Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Programming puzzle / quiz.  (Read 2772 times)

0 Members and 1 Guest are viewing this topic.

Critkeeper

  • Jr. Member
  • **
  • Posts: 62
    • View Profile
Programming puzzle / quiz.
« on: June 21, 2015, 03:30:20 pm »
suppose i have a tuple containing tuples and arrays

Code: [Select]
tuple
< tuple
< map<int, int>
, float
>
, tuple
< int
, int
>
, array
< int
5
>
, tuple
< vector<int>
, queue<float>
>
>
state;

the normal way to access an element in a tuple is to use

Code: [Select]
get <Index> (tuple);
so for the above, to access the array I would use

Code: [Select]
get <2> (state);
but in order to access the 3rd element of the array I would have to say:

Code: [Select]
get <3> ( get <2> (state) );
this is quite ugly.


here is an imaginary function that looks prettier:

Code: [Select]
get <3> (state, 2);
what would the full function signature for such a function look like?
« Last Edit: June 21, 2015, 03:31:55 pm by Critkeeper »
if(CritKeeper.askQuestion()){return question.why();}

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: Programming puzzle / quiz.
« Reply #1 on: June 21, 2015, 04:07:17 pm »
You can't do that because 2 is a template parameter which need to be known at compile time. You might do something with some template magic but honestly... why so many tuples? A struct would be way much simpler...
SFML / OS X developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Programming puzzle / quiz.
« Reply #2 on: June 21, 2015, 04:09:16 pm »
The index must be a constant expression. You can write a function template func<N, M>(tuple) that wraps get<M>(get<N>(tuple)). But as Hiura says, it's going to be ugly, especially since you mix tuples with arrays. Try to find an easier solution.

And can we please stop this bad habit of posting everything in General Discussions? This subforum clearly revolves around discussions about the library, not help requests. Your topic is not even related to SFML ::)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: Programming puzzle / quiz.
« Reply #3 on: June 21, 2015, 04:13:59 pm »
Ho, and I remember now: I've read something along those line recently. Here: http://kirkshoop.github.io/2015/06/16/user_defined_literal__idx_and_operator_for_tuple_pair_and_any.html
SFML / OS X developer

Critkeeper

  • Jr. Member
  • **
  • Posts: 62
    • View Profile
Re: Programming puzzle / quiz.
« Reply #4 on: June 21, 2015, 04:31:40 pm »
The index must be a constant expression. You can write a function template func<N, M>(tuple) that wraps get<M>(get<N>(tuple)). But as Hiura says, it's going to be ugly, especially since you mix tuples with arrays.

Code: [Select]
template <int s, int i, class... Types>
typename tuple_element <s, tuple_element<i, tuple<Types...>>&>::type&
get ( tuple <Types...>& T )
{
    return get <i> (get <s> (T));
}
« Last Edit: June 21, 2015, 04:34:01 pm by Critkeeper »
if(CritKeeper.askQuestion()){return question.why();}

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: Programming puzzle / quiz.
« Reply #5 on: June 21, 2015, 06:44:06 pm »
Well, you're missing at least two overloads (if we don't count the `get<type>(tuple)` versions) and both i and s should not be ints.
SFML / OS X developer