C ++编译器错误(尝试创建向量的静态向量)

时间:2011-08-24 17:50:29

标签: c++ boost

我正在尝试创建一个静态矢量向量,我发现下面的代码在gcc-4.1.2下编译并运行但在gcc-4.5.1下它无法使用消息进行编译

assign.cxx:19:48: error: no matching function for call to ‘to(boost::assign_detail::generic_list<std::basic_string<char> >&)’

任何人都能解释为什么会这样吗?如果您对如何做我正在尝试做的事情有任何其他建议,那么我会对此感到高兴:)。

#include <iostream>
#include <string>
#include <vector>
#include <boost/assign/list_of.hpp>

template <template <typename> class containerType, typename elemType>
containerType<elemType> to( boost::assign_detail::generic_list<elemType> list ) {
  containerType<elemType> tempContainer = list;
  return tempContainer;
}

static const std::vector<std::vector<std::string> > names = boost::assign::list_of
  (to<std::vector>(boost::assign::list_of<std::string>("A")("B")("C") ))
  (to<std::vector>(boost::assign::list_of<std::string>("D")("E")("F") ));

int main() {

  for( std::vector< std::vector<std::string> >::const_iterator itVec = names.begin(); itVec != names.end(); ++itVec ) {
    for( std::vector<std::string>::const_iterator itStr = itVec->begin(); itStr != itVec->end(); ++itStr ) {
      std::cout << "Value is " << *itStr << std::endl;
    }
  }
  return 0;
}

3 个答案:

答案 0 :(得分:3)

问题源于这样一个事实:在to<>()的定义中,containerType被声明只有一个模板参数,而事实上std::vector<>有两个模板参数。

这个在GCC 4.1.2中编译的事实只表明GCC 4.1.2中的一个错误 - 代码本身仍然不正确。

不幸的是,我无法想到一个好的变通办法。以下编译但限制您只有两个模板参数的容器(其中第二个是分配器),这可能不是您最终想要的:

#include <iostream>
#include <string>
#include <vector>
#include <boost/assign/list_of.hpp>

template<
  template<typename, typename> class containerType,
  typename allocatorType,
  typename elemType
>
containerType<elemType, allocatorType >
to(boost::assign_detail::generic_list<elemType> const& list)
{
  return list; // no need to explicitly construct a containerType instance
}

template<template<typename, typename> class containerType, typename elemType>
containerType<elemType, std::allocator<elemType> >
to(boost::assign_detail::generic_list<elemType> const& list)
{
  return to<containerType, std::allocator<elemType> >(list);
}

static std::vector<std::vector<std::string> > names = boost::assign::list_of
  (to<std::vector>(boost::assign::list_of<std::string>("A")("B")("C")))
  (to<std::vector>(boost::assign::list_of<std::string>("D")("E")("F")));

int main()
{
  typedef std::vector<std::vector<std::string> >::const_iterator outer_iter_t;
  typedef std::vector<std::string>::const_iterator inner_iter_t;

  for (outer_iter_t itVec = names.begin(); itVec != names.end(); ++itVec)
    for (inner_iter_t itStr = itVec->begin(); itStr != itVec->end(); ++itStr)
      std::cout << "Value is " << *itStr << '\n';
}

编辑(响应dribeas的评论):已更新以允许调用者覆盖默认分配器。

答案 1 :(得分:1)

在解决了几个问题之后,我认为最简单的解决方案不是使用模板模板参数,而是使用类型模板参数:

template <typename containerType, typename elemType>
containerType to( boost::assign_detail::generic_list<elemType> list ) {
  containerType tempContainer = list;
  return tempContainer;
}

static const std::vector<std::vector<std::string> > names = boost::assign::list_of
  (to<std::vector<std::string> >(boost::assign::list_of<std::string>("A")("B")("C") ))
  (to<std::vector<std::string> >(boost::assign::list_of<std::string>("D")("E")("F") ));

可能需要在调用端进行更多的输入,但它会处理您未在模板模板参数中声明的额外模板参数。

答案 2 :(得分:-1)

您对函数to<std::vector>( ... )的调用仅包含一个模板参数,而函数声明包含两个。我会尝试:to<std::vector, std::string>( ... )