c++ - 在类外知道模板的类型

标签 c++ templates

为了说清楚,我想模仿 std::vectorvalue_type 成员的行为。

例如:

template <class T>
class foo{
    //some declaration and definition for value_type
};

int main(){
    foo<int> bar;
    bar::value_type x=5; //x is int
}

我该如何实现?

最佳答案

尝试:

template <class T>
class foo {
public:
    typedef T value_type;
};

顺便说一句:bar::value_type 无效,您应该将其用作:

foo<int>::value_type x = 5; //x is int

关于c++ - 在类外知道模板的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34918112/

相关文章:

android - cocos2d-x中tableview的cell touch误操作

c++ - 尝试使用 strcpy 时看到应用程序崩溃

c++ - 使用 Boost.Preprocessor 来减少代码重复

c++ - 迭代器的模板参数 : function infers type when called?

php - Laravel 5.3 将数据传递到密码重置模板失败

c++ - 实现可变最小值/最大值函数

c++ - 下溢或上溢的指针会发生什么情况?

C++应用程序仅在关闭时才在串行上发送数据

c++ - 如何将模板化类作为默认模板类型传递给自己的类?

c++ - 我可以制作一个所有类都可以使用的模板 operator<< 重载吗? C++