I submitted a bug report at Microsoft connect (link). So the answer to my question is that there is a bug in my code.
The problem is that the join function returns a boost fusion joint_view, which contains the two elements seq1 and seq2. They are defined as:
template <typename Sequence1, typename Sequence2>
struct joint_view : sequence_base<joint_view<Sequence1, Sequence2> >
{
(...)
private:
typename mpl::if_<traits::is_view<Sequence1>, Sequence1, Sequence1&>::type seq1;
typename mpl::if_<traits::is_view<Sequence2>, Sequence2, Sequence2&>::type seq2;
};
The problem with my code is that I pass a temporary object (returned from make_vector) to the join function. A boost fusion vector is not a view and seq1 and seq2 are references. So the join function returns a joint_view containing a reference to a temporary object, which is invalid. There are two modifications to solve this:
First solution (same for createVector):
template <typename T> vector<int&, double&> createVector2(T vec, double& v2) { auto x = make_vector(ref(v2)); auto newVector = join(vec, x); return newVector; }
Now join returns a joint_view which contains a reference to x, which is valid. At the end, the view is converted to a boost fusion vector and the references are resolved.
Second solution (same for createVector):
template <typename T> vector<int&, double&> createVector2(T vec, double& v2) { return join(vec, make_vector(ref(v2))); }
The lifetime of the temporary object (returned by make_vector) is the whole return statement and as in the first version, the view will be converted to a boost fusion vector and the references are again resolved.
Thanks to Eric Brumer (Microsoft) who provided these two solutions.
Conclusion: Do not pass a temporary object to the boost fusion join function (only if it is another view).