具有通用/模板化变量的 STL 容器

发布于 2024-10-14 20:52:33 字数 343 浏览 2 评论 0 原文

我只想执行以下操作:

template <typename T>
class gvar {
private:
    T var;
public:
    gvar(T var) : var(var) {}
};

std::stack<gvar> some_stack;

g++ 吐出有关 gvar 不是类型的各种错误。这可以通过某种相对简单的方式实现吗?我不想使用 boost::any / boost::variant。

编辑:

为了澄清我想要什么:

一个可以保存不同类型变量的 std::stack (只是基元就可以了)。

I would simply like to do something of the following:

template <typename T>
class gvar {
private:
    T var;
public:
    gvar(T var) : var(var) {}
};

std::stack<gvar> some_stack;

g++ spits out all kinds of errors about how gvar is not a type. Is this achievable in some relatively easy way? I prefer not to use boost::any / boost::variant.

edit:

To clarify on what I want:

An std::stack that can hold variables of different types (just primitives is fine).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

べ映画 2024-10-21 20:52:33

因为 gvar 不是类型,它是类型模板。您需要为其指定一个模板参数:

std::stack< gvar<int> > some_stack;

Because gvar isn't a type, it's a type template. You need to specify a template argument for it:

std::stack< gvar<int> > some_stack;
星光不落少年眉 2024-10-21 20:52:33

您必须实例化模板类或以其他方式处理它。例如,您可以创建一个 std::stack >,或者您可以尝试诸如 Boost::Any

You have to either instantiate the template class or approach this in some other way. For example, you can make an std::stack<gvar<int> >, or you could try solutions such as Boost::Any.

陈独秀 2024-10-21 20:52:33

您只需指定实例化 gvar 的类型,例如:

std::stack<gvar<int> > some_stack;

You simply need to specify what type to instantiate gvar over, something like:

std::stack<gvar<int> > some_stack;
阳光下的泡沫是彩色的 2024-10-21 20:52:33

一般来说,如果您想要多态性,您可以使用基类:

class GVarBase
{
public:
  virtual ~GVarBase() {}
};

template <typename T>
class GVar: public GVarBase
{
public:
private:
  T mVar;
};

std::stack< std::unique_ptr<GVarBase> > stack;

请注意,使用您当前的代码,甚至 std::stack GVar > 不起作用,需要一个默认构造函数。

In general, if you want polymorphism, you'd use a base class:

class GVarBase
{
public:
  virtual ~GVarBase() {}
};

template <typename T>
class GVar: public GVarBase
{
public:
private:
  T mVar;
};

std::stack< std::unique_ptr<GVarBase> > stack;

Note that with the current code you have, even std::stack< GVar<int> > would not work, a default constructor is required.

邮友 2024-10-21 20:52:33

如果您可以忍受有限的跨度,那就是您完全了解可以使用 元组。当然,您还需要提前知道类型。

boost::tuple<gvar<double>,gvar<int>,gvar<double> > myItems;

在您的情况下,最好简单地表达为:

boost::tuple<double,int,double> myItems;

If you can suffer with a limited span, that is you know full well the length of the collection you can look at using tuples. Of course, you're also going to need to know the type in advance.

boost::tuple<gvar<double>,gvar<int>,gvar<double> > myItems;

Which in your case would probably be best expressed simply as:

boost::tuple<double,int,double> myItems;
姐不稀罕 2024-10-21 20:52:33

不,没有办法做你想做的事。更清楚地了解您试图通过此尝试解决的问题可能会让我提供解决方法。

No. There's no way to do what you're trying to do. More clarity on the problem you're trying to solve with this attempt might allow me to provide a workaround.

甜味拾荒者 2024-10-21 20:52:33

似乎你需要这样的东西:(

class gvar {
        struct base_val { 
                virtual ~base_val() {} 
        };
        template <class T>
        struct con_value : base_val{
                con_value(T val): value(val) {}
                T value; 
        };      
        base_val *var;
public:
        template <typename T>
        gvar(T var) : var(new con_value<T>(var)) {}
        template <typename T>
        T* getValue() { 
                con_value<T> *ptr = dynamic_cast<con_value<T>*>(var);
                if(ptr == NULL) return NULL;
                return &ptr->value;
        }
};

std::stack<gvar> some_stack;

链接到 ideone 和工作示例 http://www.ideone.com/gVoLh)

这是简化的 boost::any,所以如果可以的话,只需使用 boost::any 而不是这个。

Seems you need something like this:

class gvar {
        struct base_val { 
                virtual ~base_val() {} 
        };
        template <class T>
        struct con_value : base_val{
                con_value(T val): value(val) {}
                T value; 
        };      
        base_val *var;
public:
        template <typename T>
        gvar(T var) : var(new con_value<T>(var)) {}
        template <typename T>
        T* getValue() { 
                con_value<T> *ptr = dynamic_cast<con_value<T>*>(var);
                if(ptr == NULL) return NULL;
                return &ptr->value;
        }
};

std::stack<gvar> some_stack;

(link to ideone with working example http://www.ideone.com/gVoLh)

This is simplified boost::any, so if you can, just use boost::any instead of this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文