简体   繁体   中英

“dynamic template” idiom in C++, is there a standard name for this?

I want to a template with value parameter, except the value isn't known at runtime. For example, implementing integers modulo some number n. Here is some rough C++ pseudocode:

class zmodn
{
  int v; // the value, 0 <= v < n, n below
  zmodn (int v_) : v(v_) { }
  bool operator == (const zmodn &b) const { return v == b.v; }
  ...
};

class zmodn_dyntmpl
{
  int n;
  zmodn_dyntmpl (int n_) : n(n_) { }

  zmodn create (int v_) { return new zmodn (v % n); }
  zmodn add (zmodn a, zmodn b) { return zmodn ((a.v + b.v) % n); }
  ...
};

Here, the class zmodn_dyntmpl basically represents a dynamic or run-time template. In the same way instantiating a template gives a class, instantiating it gives a "dynamic class" whose "instances" are objects of type zmodn .

My question is, is there a standard name for this idiom? What are the proper name for a class representing a dynamic template and its instances?

In the same way instantiating a template gives a class, instantiating it gives a "dynamic class" whose "instances" are objects of type zmodn.

The standard pattern that fits this description is called the Factory.

Yeah, its called a normal class.

Templates are templates, because they are "code templates", they only exist in compile time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM