0

I have a boost::variant: B is an incomplete type so I have two possibilitys to declare my variant

typedef boost::variant<B*, char, int> vari; // this works
typedef boost::variant<B&, char, int> vari; // this works also

But now I have the following function in which I try to assign a value to my variant:

void setVari(vari& v, const int n){
    swich(n) {
    case 1 :
        vari = 100;
        break;
    case 2 :
        vari = 'a'
        break;
    case 3 : {
        B elem;
        vari = elem;  // or "vari = *elem" if i use the pointer variant
        break;
        }
    case default :
        break;
    }
}

This function only works if I use the first method to declare my variant.

If I use the reference content, the compiler complains about template argument deduction/substituiion failed errors.

Is there a possibility to make this function work, when using reference instead of a pointer?

sehe
  • 374,641
  • 47
  • 450
  • 633
Ventu
  • 780
  • 1
  • 12
  • 25
  • 2
    What, exactly is it you are trying to achieve? The struct definition is never going to work. Ever. I know a few patterns with Boost Variant that solve nearly any task, but I fail to grasp which task is your goal. (Instead of showing the failing approach, describe the goal you are trying achieve?) – sehe Mar 06 '15 at 10:56
  • For now: see the restrictions on (move)assignment listed [here](http://www.boost.org/doc/libs/1_57_0/doc/html/variant/reference.html): _Assignable: variant is itself Assignable if and only if every one of its bounded types meets the requirements of the concept. (Note that top-level const-qualified types and reference types do not meet these requirements.)_ etc. – sehe Mar 06 '15 at 11:12
  • Changed question.. hope its now more understandalbe. Can someone tell me, why its downvoted? Is still anything unclear? – Ventu Mar 06 '15 at 11:15
  • Mmm. Not sure. Why are you assigning a reference to a local temporary? Also, I think you meant `*elem if I use the _reference_ variation` – sehe Mar 06 '15 at 11:17
  • Yea, maybe, but i can not tell you, because it does not compile so i can not say if it does what i want. At first i used the `pointer` variation, but then my boss wants to know, why i do not use `references` and i cant tell him: Becaus it does not work.. So i have to find a solution with using `references` or i have to explain why i use pointers. So i have to understhand the whole thing.. – Ventu Mar 06 '15 at 11:21
  • The downvote is because nobody knows what you're trying to do or why. Why are you stuck with an incomplete type? It's sending you down a blind alley with variants containing pointers/references which is just weird. Instead, attack the problem at its source: make the type fully known so you may use it directly. (_I_ wouldn't downvote this, mind you.) – Lightness Races in Orbit Mar 06 '15 at 12:55

1 Answers1

2

Variants can contain references, but that makes them non-assignable.

docs:

  • Assignable: variant is itself Assignable if and only if every one of its bounded types meets the requirements of the concept. (Note that top-level const-qualified types and reference types do not meet these requirements.)
  • MoveAssignable: variant is itself MoveAssignable if and only if every one of its bounded types meets the requirements of the concept. (Note that top-level const-qualified types and reference types do not meet these requirements.)

You can still do useful things with them (such as, return them). See e.g.: boost variant copy semantics

Community
  • 1
  • 1
sehe
  • 374,641
  • 47
  • 450
  • 633