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

后端 未结 7 1490
北荒
北荒 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:24

    I tend to like simple solutions, so I would go for the free-function approach, possibly adding SFINAE to disable the function for types other than Aggregate:

    template 
    typename std::enable_if< std::is_same::type 
                                      >::value
                           >::type
    visit( Visitor & v, T & s ) {  // T can only be Aggregate or Aggregate const
        v(s.i);
        v(s.d);   
    }
    

    Where enable_if, is_same and remove_const are actually simple to implement if you don't have a C++0x enabled compiler (or you can borrow them from boost type_traits)

    EDIT: While writing the SFINAE approach I realized that there are quite a few problems in providing the plain templated (no SFINAE) solution in the OP, which include the fact that if you need to provide more than one visitable types, the different templates would collide (i.e. they would be as good a match as the others). By providing SFINAE you are actually providing the visit function only for the types that fulfill the condition, transforming the weird SFINAE into an equivalent to:

    // pseudocode, [] to mark *optional*
    template 
    void visit( Visitor & v, Aggregate [const] & s ) {
       v( s.i );
       v( s.d );
    }
    

提交回复
热议问题