C++ template to cover const and non-const method

后端 未结 7 1506
北荒
北荒 2020-12-29 22:48

I have a problem with duplication of identical code for const and non-const versions. I can illustrate the problem with some code. Here are two s

7条回答
  •  醉话见心
    2020-12-29 23:23

    Another solution - require the Visitor class to have a metafunction that adds const when it applies:

    template 
    static void visit(Visitor &v, typename Visitor::ApplyConst::Type &a)
    {
        v(a.i);
        v(a.d);
    }
    

提交回复
热议问题