Since the number of columns is only known at runtime (it's not part of the type), you need to specify it when you create the matrix ret as follow:
template<typename Derived>
typename Derived::PlainObject bar(const Eigen::MatrixBase<Derived>& v)
{
typename Derived::PlainObject ret(v.rows(), v.cols());
std::cout << "v size : " << v.rows() << ", " << v.cols() << std::endl;
std::cout << "ret size: " << ret.rows() << ", " << ret.cols() << std::endl;
return ret;
}
After this change you get the expected answer in both cases:
v size : 3, 1000
ret size: 3, 1000
v size : 3, 1000
ret size: 3, 1000