为什么在GCC 4.4.1中我不能在函数模板中声明默认参数?

时间:2010-11-03 10:36:23

标签: c++ gcc

  1. 在这里: http://gcc.gnu.org/projects/cxx0x.html
    他们说fnc中的dflt tmp args是由他们的4.4版本支持的。我运行ver 4.4.1,当我尝试编译它时:
  2. d - 猜猜这是什么? ;)

     #include<vector>
    
        template<class Key, class CollT = std::vector>
        CollT* delete_(Key kValue)
        {
            return new CollT;
        }
    
        int main()
        {
            return 0;
        }
    

    我收到了一个错误。 那是怎么回事?

2 个答案:

答案 0 :(得分:3)

std :: vector不是一个类,它是一个模板。您可以使用语法将模板指示为模板参数。在您的情况下,您可能只想将其设为std::vector<Key>

答案 1 :(得分:0)

我认为您展示的代码已损坏: 这里是修复和如何构建,这适用于g ++ 4.4.5所以仔细检查你的版本:

#include<vector>
template <class Key, class CollT = std::vector<Key> >
CollT* delete_(Key kValue)
{
    return new CollT;
}

int main()
{
    return 0;
}

构建:

g ++ templdef.cpp -std = c ++ 0x

编辑基于评论:

   1) replace typename with class within template definition (both class CollT and typename CollT seems fine)
   2) replaced `vector<int>` with `vector<Key>`
   3) compiling without flag, will give you the following error: default template arguments may not be used in function templates without -std=c++0x or -std=gnu++0x