1

I can't understand why the code behaves this way?

#include <iostream>

#include <boost/fusion/container/vector.hpp>
#include <boost/fusion/include/vector.hpp>
#include <boost/fusion/container/vector/vector_fwd.hpp>
#include <boost/fusion/include/vector_fwd.hpp>
#include <boost/fusion/container/generation/make_vector.hpp>
#include <boost/fusion/include/make_vector.hpp>
#include <boost/fusion/sequence/io.hpp>
#include <boost/fusion/include/io.hpp>

template<typename Ar>
void func(Ar& ar, const boost::fusion::vector<>& v) {
   std::cout << v << std::endl;
}

template<typename Ar, typename T0, typename T1>
void func(Ar& ar, const boost::fusion::vector<T0, T1>& v) {
   std::cout << v << std::endl;
}

struct type {
   template<typename T>
   type& operator& (const T& v) {
      func(*this, v);
      return *this;
   }
};

int main() {
   type t;
   t & boost::fusion::make_vector(33,44); // 1. <<<<<<<<<<<<<<<<<<<<<<<<

   boost::fusion::vector<int, int> v(55,66); // 2.
   t & v;
}

test code here

The question is, why in the first case the func() for an empty vector is called?

Documentation on this topic:

boost::fusion::vector

boost::fusion::make_vector()

Thank's.

Luc Touraille
  • 79,925
  • 15
  • 92
  • 137
niXman
  • 1,698
  • 3
  • 16
  • 40

1 Answers1

3

This is roughly what I understand...

boost::fusion::make_vector() from your usage of boost::fusion::make_vector(33,44) returns a boost::fusion::vector2<int, int> type and NOT a boost::fusion::vector<int, int, T2, T3,...> (variadic) type. boost::fusion::vectorN types can however convert itself to boost::fusion::vector<> (variadic) type.

The first function accepts a variadic vector with NO type. Hence no elements are displayed. The second version accepts a variadic type with two template types declared, however since the first one matches better (because the default template types kick in) it gets choosen over the second one, when you use boost::fusion::make_vector. When you define the type of the vector as in the second case, it has strong type specified and hence matches the second function and displays two elements of type int and int.

Vite Falcon
  • 6,575
  • 3
  • 30
  • 48
  • This makes sense, and if you change to `vector` in the first function, it displays the values. – Gustav Larsson May 05 '11 at 15:08
  • I've changed my code: http://liveworkspace.org/code/885e6909e74256038708708746d098e7 Now the second variant doesn't work. Any ideas? – niXman May 05 '11 at 17:07
  • Yup... because 'variadic' type does not convert to `vector1`, `vector2`, etc. It simply converts itself to the default `vector0` type, if it needs conversion and hence matches the vector0 function. However, if you specify the functions like this: http://liveworkspace.org/code/3a0832f4f36d23626636fab12f0845dd. The compiler finds an easy fit and doesn't convert it to any of the specified vectorN classes. It will work upto the numbr of template arguments specified for the function, i.e as per the code I've shown it will work for vector<>, vector, vector; not for vector. – Vite Falcon May 06 '11 at 09:40