标准::向量声明中使用的模板参数
Template parameters used in std::vector declaration
我正在从这里阅读有关std::vector
类的信息。 我对使用的模板参数有点困惑。使用的声明是:
template<
class T
class Allocator = std::allocator<T>
> class vector;
参数class Allocator = std::allocator<T>
让我感到困惑。
这是模板模板参数的示例吗?
但是,我认为它可能适合我在这里找到的Type template parameter
type-parameter-key name(optional) = default
。
我尝试了以下实验,但它给出了编译错误:
d1 的值在常量表达式中不可用
#include<iostream>
#include<stdio.h>
#include<string>
using namespace std;
//Type template parameter
template<class T>
class foo{
T a;
public :
void sayHello(){
cout<<"Say Hello to : "<<a<<endl; //Would have taken care of the overloaading << , if there were no other errors
}
void setVal(T temp){
this->a = temp;
}
};
class dog{
string name ="labrador";
};
int main(){
dog d1;
//foo<d1> foo_1; ////Does not work
return 0;
}
如何使上述代码工作?
http://www.cplusplus.com/doc/oldtutorial/templates/简单的语法错误伙伴,传递类型名称,而不是类型实例标识符。 为了使它编译,我做了一个快速实现的 ostream 重载。
#include<iostream>
#include<stdio.h>
#include<string>
using namespace std;
//Type template parameter
template<class T>
class foo{
T a;
public :
void sayHello(){
cout<<"Say Hello to : "<<a<<endl; //Would have taken care of the overloaading << , if there were no other errors
}
void setVal(T temp){
this->a = temp;
}
};
class dog{
string name ="labrador";
friend ostream& operator<<(ostream& os, const dog& dt);
};
ostream& operator<<(ostream& os, const dog& dt)
{
os << "dog stuff" << endl;
return os;
}
int main(){
dog d1;
foo<dog> foo_1; ////Does not work
foo_1.sayHello();
return 0;
}
相关文章:
- 我使用向量来创建类对象列表.初始化向量时如何使用参数调用构造函数?
- 标准::向量声明中使用的模板参数
- 使用 C++ 将命令行参数拆分为参数/向量
- 如何在 c++ 中定义接受不同参数类型的函数向量?
- 如何将向量中的可变参数转换为参数的持有者?
- C++向量默认为成员参数
- 使用向量初始化参数化构造函数的对象数组
- 有什么方法可以将具有不同模板参数的模板类实例放入向量中?
- OpenCL 是否支持向量作为内核参数?
- 使用并行参数向量调用元素向量的成员函数
- 如何将子类作为函数的参数传递给期望基类,然后将该对象传递到指向这些抽象类对象的指针向量中?
- 将不同类型的模板参数包提取到双精度向量中会产生警告
- 使用参数的可变大小向量格式化字符串(例如,将参数向量传递给 std::snprintf)
- 使用参数向量调用函数
- 合并排序 c++ 1 参数 - 向量
- 快速排序函数C++ 1 个参数 - 向量
- RAII字符**用作exec参数向量
- C++模板,它调用带有参数向量的函数
- std::async - 参数向量已损坏
- 在C++中调用可变参数函数,参数向量