-4

Original post:

I am having an object in a complex project. So I may not be able to provide very detail API of all these classes, but I am trying to abstract my problem without need of detail.

First of all, the code below works fine:

auto ha = std::get< 1 >( d.data ).data;

Then I am try to have code below:

auto ha = std::get< 1 >( d.data ).data;
int ma = ha;

The compiler says

cannot convert ‘std::tuple<>’ to ‘int’ in initialization int ma = ha;
cannot convert ‘std::tuple<PointStreamColor<float> >’ to ‘int’ in initialization

This should means that ha is a std::tuple variable. Then I have code below:

auto ha = std::get< 1 >( d.data ).data;
auto ma = std::get< 0 >( ha );

For the code above, the compiler says

no matching function for call to ‘get(std::tuple<>&)’ int ma = std::get< 0 >( ha );

I cannot understand why can't I use std::get here to reach the first element of a std::tuple? Can anyone give me some idea?

Update

Thank you all for pointing out that I should provide more info. I totally understand this before I post. But the implementation of classes here are template based, so massive and hierarchical, that I am not able to provide them all.

So I was trying to ask for any general idea that might cause failure for reaching std::tuple element by std::get.

Maybe the problem I am facing is not suitable for public discusstion here. I am so sorry for taking your time. I cannot delete this question now and maybe one of you can help close this question now.

Thank you for your time.

Summer Sun
  • 947
  • 13
  • 33
  • 6
    What is `d`? , what is `data`? You need to provide a [MCVE]. – Jabberwocky Jul 25 '18 at 15:19
  • 2
    "Can anyone give me some idea?" Don't you think it would be a good idea to at least provide that tuple definition? Our telepathy machine is broken today, sorry. – Slava Jul 25 '18 at 15:19
  • 1
    Guessing at what variables are with error messages is a... _curious_ way of programming . – Passer By Jul 25 '18 at 15:21
  • @Jabberwocky Thank you Jabberwocky because I thought std::get should work for tuple anyway, so I did not attach the detail. I will add it soon. – Summer Sun Jul 25 '18 at 15:25
  • @SummerSun OK, and please keep it minimal. – Jabberwocky Jul 25 '18 at 15:26
  • I'm sorry @Jabberwocky and all, I fail to provide detail info due to the complexity of codes. Maybe you can help me close this question now. I apologize for posting this stupid question. – Summer Sun Jul 26 '18 at 05:39

1 Answers1

4

You can't get the zeroth element from a tuple with no elements.

It's the same concept as indexing an empty array at 0.

EDIT: To clarify, I suspect your code is being called twice. The line

auto ha = std::get< 1 >( d.data ).data;
int ma = ha;

gives two error messages, because in both cases, ha is a tuple, which can't cast to an int:

cannot convert ‘std::tuple<>’ to ‘int’ in initialization int ma = ha;
cannot convert ‘std::tuple<PointStreamColor<float> >’ to ‘int’ in initialization

This shows in the first case, ha is an empty tuple. In the second case, ha has one element.

The alternative code

auto ha = std::get< 1 >( d.data ).data;
auto ma = std::get< 0 >( ha );

only results in one error message because std::get< 0 >( ha ) is perfectly valid in the second case, when ha is type std::tuple<PointStreamColor<float> > which has one element. This error message arises from the first case, when ha is type std::tuple<> which is empty:

no matching function for call to ‘get(std::tuple<>&)’ int ma = std::get< 0 >( ha );
Taylor Nichols
  • 588
  • 2
  • 13