[C++-sig] template constructor problem

Ralf W. Grosse-Kunstleve rwgk at cci.lbl.gov
Sat Jan 19 12:35:16 CET 2002


The following code compiles fine (e.g. with gcc 3.0.3 or Compaq cxx 6.2)
as shown. However, if the constructor for struct stats is turned into
a template constructor (move the //), gcc reports:

template_constructor.cpp:20: no matching function for call to
`vector_min(const 
   boost::python::detail::reference_parameter<std::vector<float, 
      std::allocator<float> >&>&)'

I assume because reference_parameter<> does not have a typename value_type.
Is there an easy way to get around this problem?

Thanks,
        Ralf

P.S.: My best workaround involves inheriting from
std::vector<float>, class_build both std::vector<float> and the
inheriting type, declare_base, define the constructor only
for the inheriting type...


#include <boost/python/class_builder.hpp>
#include <vector>

template <typename VectorType>
typename VectorType::value_type
vector_min(const VectorType& v)
{
  typename VectorType::value_type result = v[0];
  for(std::size_t i=1;i<v.size();i++) {
    if (result > v[i]) result = v[i];
  }
  return result;
}

template <typename ValueType>
struct stats
{
  //template <typename VectorType> stats(const VectorType& vec) {
  stats(const std::vector<float>& vec) {
    m_min = vector_min(vec);
  }
  ValueType m_min;
};

BOOST_PYTHON_MODULE_INIT(template_constructor)
{
  boost::python::module_builder this_module("template_constructor");

  boost::python::class_builder<std::vector<float> > py_svf(this_module, "svf");
  py_svf.def(boost::python::constructor<>());

  boost::python::class_builder<stats<float> > py_stats(this_module, "stats");
  py_stats.def(boost::python::constructor<std::vector<float>&>());
}




More information about the Cplusplus-sig mailing list