Avoid switching on template parameters

前端 未结 3 924
情书的邮戳
情书的邮戳 2021-01-24 18:02

Simplified I have the following class hierarchy:

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

    virtual double get_double(int i) con         


        
3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-24 18:45

    Why not change foo_template to be:

    template
    double foo_template(Vec*) {
      // use v which involves calling get
      return result;
    }
    

    and foo to be:

    template
    double foo (Vec* v )
    {
    return foo_template(v)
    }
    

    and let argument deduction do the work?

    (You can probably get rid of one of the functions, but I wanted to keep is as close to the original)

提交回复
热议问题