0

I learned about std::forward_as_tuple. I use this tech in my project:

template<class... Ts>
tuple<Ts&&...> values(Ts&&... ts) // yes, values is renamed forward_as_tupe
{
    return tuple<Ts&&...>(std::forward<Ts>(ts)...)
}

class C_SQL { ...... }; // MySQL C API, SQL statement encapsulation
                        // operator% is well defined.

char pszname[32];
int  iage;

C_SQL sql;
sql % "insert into student (name,age,grade)"
    %  values(pszname, iage, 2 );

Then, I can get a MySQL C API Prepared Statement string like this:

"insert into student (name,age,grade) values(?, ?, 2 )"

"pszname" and "iage" are lvalues, so they are binding variables. 2 is an rvalue, so I can tell that it's a literal in an SQL statement.

My question is:

How can I tell if an element in a tuple is an rvalue or an lvalue?

For example:

int k;
tuple<int&, int&&> t = forward_as_tuple(k, 2);

The first element is an lvalue-reference, the second is an rvalue-reference.

Please use C++ code to tell that. The code can deal with varaidic template tuple.

David G
  • 94,763
  • 41
  • 167
  • 253
thomas
  • 505
  • 3
  • 12
  • Sorry for the multiple edits! Now if I understand correctly, you want to detect if the elements in a tuple are an lvalue or an rvalue. What exactly are you planning to do with this information? – David G Dec 28 '13 at 00:46
  • @0x499602D2: As I understand, it is to fill `values(..)` content. (`?` or direct value). – Jarod42 Dec 28 '13 at 00:50
  • @Jarod42 is always right. This question also indicates that rvalue reference element of tuple is not useless – thomas Dec 28 '13 at 01:09

2 Answers2

3

you may use

std::is_rvalue_reference<std::tuple_element<Index, MyTuple>::type>::value
std::is_lvalue_reference<std::tuple_element<Index, MyTuple>::type>::value
Jarod42
  • 203,559
  • 14
  • 181
  • 302
2

You have a lot of useful traits in #include <type_traits>. You are probably interested in std::is_rvalue_reference and std::is_lvalue_reference.

For example, to check if the first type is an rvalue:

bool r0 = std::is_rvalue_reference<std::tuple_element<0, decltype(t)>::type>::value;
rodrigo
  • 94,151
  • 12
  • 143
  • 190