非静态模板成员:可能吗? [英] non-static template member : possible?

查看:96
本文介绍了非静态模板成员:可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在类中创建非静态模板字段?

如果没有,如何解决?

Is it possible to create non-static template field in a class?
If no, how to workaround?

应在编译时根据需要创建此类字段。

Such fields should be created at compile time as needed.

我有很多 B -class,例如 B1 B2 B3 。 br>
(实际上,它们具有更有意义的名称。)

I have a lot of B-class, like B1,B2,B3.
(In real case, they have more meaningful names.)

我想创建一个类 D 具有非静态模板功能 add< BX>()每次调用c> counter ++ 时,对于每个 BX ,对于D的某个实例。

(在实际情况下,它的操作要复杂一些。)

I want to create a class D that has non-static template function add<BX>() that have to counter++ every time I call it, for each individual BX, for a certain instance of D.
(In real case, it does somethings more complex.)

这是一个有效的 演示 来实现。

可悲的是,我目前必须对每个<$ c进行硬编码$ c> BX ,一对一( B1 B2 B3 )内 D :-

Here is a working demo to achieve it.
Sadly, I currently have to hard-code every BX, one-by-one (B1,B2,B3) inside D :-

class B1{};class B2{};class B3{};
class Counter{
    public: int counter=0;
};
template<class BX>class Tag{};
class D{
    Counter countB1;
    Counter countB2;
    Counter countB3;
    public: template<class BX> void add(){  
        add_(Tag<BX>());
    }
    private:
    void add_(Tag<B1>){ countB1.counter++;}
    void add_(Tag<B2>){ countB2.counter++;}
    void add_(Tag<B3>){ countB3.counter++;}
    public: template<class BX> int get(){
        return get_(Tag<BX>());
    }
    private:
    int get_(Tag<B1>){  return countB1.counter;}
    int get_(Tag<B2>){  return countB2.counter;}
    int get_(Tag<B3>){  return countB3.counter;}
};

这里是用法。请注意,每个 D 实例都保留自己的 counter :-

Here is the usage. Notice that each instance of D keep its own counter :-

int main() {
    D d1;
    d1.add<B2>();   d1.add<B2>();   d1.add<B3>();
    std::cout<<d1.get<B1>()<<" "<<d1.get<B2>()<<" "<<d1.get<B3>()<<"\n";
    //^ print 0 2 1  
    D d2;
    d2.add<B1>();
    std::cout<<d2.get<B1>()<<" "<<d2.get<B2>()<<" "<<d2.get<B3>()<<"\n";
    //^ print 1 0 0  (not 1 2 1)
    return 0;
}

我梦for以求:-

class D{
    Counter<BX> countBX; //???
    public: template<class BX> void add(){  
         Counter<BX>::getNonStaticInstance(this).counter++; //???
    }
    public: template<class BX> int get(){
        return Counter<BX>::getNonStaticInstance(this).counter; //???
    }
};

我知道如果 countBX 怎么办是静态的,但对于非静态的似乎是不可能的。

I know how to do it if countBX is static, but for non-static it seems to be impossible.

推荐答案

使用 std :: map std :: unordered_map (来自Yakk的建议;谢谢)的索引和RTTI?

Using a std::map std::unordered_map (suggestion from Yakk; thanks) of indexes and RTTI?

#include <map>
#include <iostream>
#include <typeindex>

class B1 {};
class B2 {};
class B3 {};

class D
 {
   private:
      std::unordered_map<std::type_index, std::size_t> bxMap;

   public:
      template <typename BX>
      void add ()
       { ++ bxMap[std::type_index(typeid(BX))]; }

      template <typename BX>
      int get ()
       { return bxMap[std::type_index(typeid(BX))]; }
 };

int main ()
 {
   D d1;
   d1.add<B2>();    d1.add<B2>();   d1.add<B3>();
   std::cout<<d1.get<B1>()<<" "<<d1.get<B2>()<<" "<<d1.get<B3>()<<"\n";
   //^ print 0 2 1
   D d2;
   d2.add<B1>();
   std::cout<<d2.get<B1>()<<" "<<d2.get<B2>()<<" "<<d2.get<B3>()<<"\n";
   //^ print 1 0 0
   return 0;
 }

这篇关于非静态模板成员:可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆