Need help with C++ tuple values please

I am writing Arm C++ embedded program using C++ map STL, with two keys pair and three values tuple.

And the function is crashing because the compiler do not allow exception handling calls by
get and tie functions of C++.

I will give details in my next reply please.


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
32
33
34
35
36
37
38
typedef pair<int, int> pair_key;
typedef tuple<int, int, int> tuple_vals;
typedef map< pair<int, int>, tuple<int, int, int> > pair_tuple_map_type;


pair_tuple_map_type lookup_pins
{       // Brd, BNK, srclk, rclk, ser_data
        {   {1,0}, {(PD_13), (PD_12),  (PE_15)}     },  { {1,1}, {(PD_13), (PD_12),  (PF_13)} },
        {   {2,0}, {(PD_14), (PD_15),  (PF_14)}     },  { {2,1}, {(PD_14), (PD_15),  (PF_15)} },
};

void set_to_fast()
{
    serial_str_write("\n");
    serial_str_write( "Setting to fast:\n" );

    int sr, rc, s0;

    for (const auto &lookup_row : lookup_pins)
    {
        auto key_pair = lookup_row.first;
        // auto [srclk, rclk, ser_data] = lookup_row.second;

        static auto map_vals = lookup_row.second;
        // static int sr = get<0>(map_vals);
        // static int rc = get<1>(map_vals);
        // static int s0 = get<2>(map_vals);

        tie(sr, rc, s0) = map_vals;

        serial_str_write("\n");    serial_str_write( "Sr: " );    serial_int_write(sr);  serial_str_write("\n"); 
        serial_str_write("\n");    serial_str_write( "Rc: " );    serial_int_write(rc);  serial_str_write("\n"); 
        serial_str_write("\n");    serial_str_write( "S0: " );    serial_int_write(s0);  serial_str_write("\n"); 
    }

}

Last edited on
I have commented out the lines that throw exceptions and the function gives following output on the Serial Terminal


Type X to LED ON
Type y to LED OFF

Setting to fast:

Prior to the closing curly brace of for const auto &lookup  :

Prior to the closing curly brace of for const auto &lookup  :

Prior to the closing curly brace of for const auto &lookup  :

Prior to the closing curly brace of for const auto &lookup  :

Type X to LED ON
Type y to LED OFF



Last edited on
set_to_fast function with commented out code, that is giving above output is shown below:

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
void set_to_fast()
{
    serial_str_write("\n");
    serial_str_write( "Setting to fast:\n" );

    int sr, rc, s0;

    for (const auto &lookup_row : lookup_pins)
    {
        auto key_pair = lookup_row.first;
        // auto [sr, rc, ser0] = lookup_row.second;

 //        static auto map_vals = lookup_row.second;
        // static int sr = get<0>(map_vals);
        // static int rc = get<1>(map_vals);
        // static int s0 = get<2>(map_vals);

        // tie(sr, rc, s0) = map_vals;
/*
        serial_str_write("\n");    serial_str_write( "Sr: " );    serial_int_write(sr);  serial_str_write("\n"); 
        serial_str_write("\n");    serial_str_write( "Rc: " );    serial_int_write(rc);  serial_str_write("\n"); 
        serial_str_write("\n");    serial_str_write( "S0: " );    serial_int_write(s0);  serial_str_write("\n"); 
*/

        serial_str_write("\n");
        serial_str_write( "Prior to the closing curly brace of for const auto &lookup  :\n" );
    }

}
Need your help with C++ code that gives tuple values but should use code statements that do not throw exceptions please.

Thanks and best regards,
Last edited on
I have searched on Google about finding the values of tuple using C++ functions that do not throw exceptions and I did not find any C++ code that can return tuple values by calling non-exception-throwing functions.

I need help in making this set_to_fast function work please.

I sincerely appreciate your time and help please.

Thanks and best regards,
If above pair_tuple_map_type lookup_pins declaration is not compilable, then
three values have to be declared as array of ints.

Any code sample please?

1
2
3
4
5
6

pair_tuple_map_type lookup_pins
{       // Brd, BNK, srclk, rclk, ser_data
        {   {1,0}, {(PD_13), (PD_12),  (PE_15)}     },  { {1,1}, {(PD_13), (PD_12),  (PF_13)} },
        {   {2,0}, {(PD_14), (PD_15),  (PF_14)}     },  { {2,1}, {(PD_14), (PD_15),  (PF_15)} },
};
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
using tup_type = int[3] ; // srclk, rclk, ser_data 
//  (Brd, BNK) mapped to (srclk, rclk, ser_data)
const std::pair< std::pair<int,int>, tup_type > lookup_pins[4] = { /* ... */ }; // array of pairs

void set_to_fast()
{
    for( const auto& [first,second] : lookup_pins )
    {
        const auto [Brd,BNK] = first ;
        const auto [srclk,rclk,ser_data] = second ;
        // ...
    }
}
Thank you very much JLBorges,

Following lookup_pins[4] initialization is giving syntax error. May I know how to fix this initialization error please?

1
2
3
4
5
6
7
8
9
typedef pair<int, int> pair_key;
using tup_type = int[3]; // srclk, rclk, ser_data

const std::pair< pair_key, tup_type > lookup_pins[4] =
{       // Brd, BNK, srclk, rclk, ser_data
        {   {1,0}, {(PD_13), (PD_12),  (PE_15)}     },  { {1,1}, {(PD_13), (PD_12),  (PF_13)} },
        {   {2,0}, {(PD_14), (PD_15),  (PF_14)}     },  { {2,1}, {(PD_14), (PD_15),  (PF_15)} }
};
Last edited on
Change the c-style array to a std::array (a copyable type)

1
2
3
4
5
6
7
8
typedef pair<int, int> pair_key;
using tup_type = std::array<int,3> ; // srclk, rclk, ser_data (#include <array>)

const std::pair< std::pair<int, int>, tup_type > lookup_pins[4] =
{       // Brd, BNK, srclk, rclk, ser_data
        {   {1,0}, {(PD_13), (PD_12),  (PE_15)}     },  { {1,1}, {(PD_13), (PD_12),  (PF_13)} },
        {   {2,0}, {(PD_14), (PD_15),  (PF_14)}     },  { {2,1}, {(PD_14), (PD_15),  (PF_15)} },
};
Hello JLBorges,

Thanks so much.

Below function is working.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void set_to_fast()
{
    serial_str_write("\n");
    serial_str_write( "Setting to fast:\n" );

    int sr, rc, s0;

    for (const auto& [first, second] : lookup_pins)
    {
        const auto [Brd, Bnk] = first;

        const auto [sr, rc, s0] = second;

        serial_str_write("\n");    serial_str_write( "Sr: " );    serial_int_write(sr);  serial_str_write("\n"); 
        serial_str_write("\n");    serial_str_write( "Rc: " );    serial_int_write(rc);  serial_str_write("\n"); 
        serial_str_write("\n");    serial_str_write( "S0: " );    serial_int_write(s0);  serial_str_write("\n"); 

        serial_str_write("\n");
        serial_str_write( "Prior to the closing curly brace of for const auto &lookup  :\n" );
    }

}
However, following three lines are giving warning by saying, this decomposition feature is available in C++17.
Is there a way to use C++14 or prior standard feature?


1
2
3
4
5
6
    for (const auto& [first, second] : lookup_pins)
    {
        const auto [Brd, Bnk] = first;

        const auto [sr, rc, s0] = second;


> this decomposition feature is available in C++17

Ignore the warning; use current C++ to the extent that this crippled implementation supports it.
(you may be able to turn this warning off with the appropriate -Wno-xxxxx compiler option.)

This is how it could be in C++14:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void set_to_fast()
{
    for( const auto& pair : lookup_pins )
    {
        const key_type& key = pair.first ;
        const int Brd = key.first ;
        const int BNK = key.second ;

        const tup_type& arr = pair.second ;
        const int srclk = arr[0] ;
        const int rclk = arr[1] ;
        const int ser_data = arr[2] ;
        // ...
    }
}
Topic archived. No new replies allowed.