rvalue variant with lvalue references does not compile
- Dominant language
- C++
- Stars
- 47
- Forks
- 71
- PR merge metrics
- No merged PRs in 30d
Description
As far as I can tell, the following is a legitimate code which attempts to a temporary variant to keep references. But it fails to compile:
```.cpp
struct Changer : boost::static_visitor {
void operator()(int& i) const {
i += 10;
}
void operator()(double& d) const {
d += 20;
}
};
auto getVar(int choose, int& i, double& d){
using V = boost::variant;
return choose == 0 ? V(i) : V(d);
}
void foo() {
int i = 0;
double d = 0;
Changer c;
auto v1 = getVar(0,i,d);
boost::apply_visitor(c, v1); // OK
assert(i == 10);
assert(d == 0.);
boost::apply_visitor(c, getVar(1,i,d)); // ERROR
assert(i == 10);
assert(d == 20.);
}
```
```
error: no matching function for call to object of type 'Changer'
```
See https://godbolt.org/z/FVVA_t for the error.
It seems `apply_visitor` is making the assumption that if a variant is rvalue, so should be the elements. But (unlike std::variant) boost::variant supports references as elements, so the two are decoupled.
This makes variants impossible to use as proxy objects.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.