template parameters, #define and code duplication

隐身守侯 提交于 2019-12-10 13:12:53

问题


I have a lot of code like this:

#define WITH_FEATURE_X

struct A {
#ifdef WITH_FEATURE_X
  // ... declare some variables Y
#endif
  void f ();
};

void A::f () {
  // ... do something
#ifdef WITH_FEATURE_X
  // ... do something and use Y
#else
  // ... do something else
#endif
  // ... do something
}

and I'd like to replace the #defines with template parameters:

template < int WITH_FEATURE_X > // can be 0 or 1
struct A;

But I don't want to duplicate almost the entire code of A::f() for A<0>::f() and A<1>::f() just for the few lines that depend on the parameter. I also don't want to call functions instead of the previous #ifdefs. What is the common solution?


回答1:


I believe what you want is an equivalent to the "static if" command that exists in D language. I am afraid such a feature does not exist in C++.

Note that if parts of your code vary depending on the feature your request, these parts don't belong in the main function because they are not part of the bare algorithm. So the option to delegate such features in functions seems like a good one.

EDIT
If your #ifdef statements are used to do the same subtask differently, then defining subfunctions is the right thing to do. It will make your code more readable, not less.

If they are used for completely different actions, well, your code is already cluttered. Do something about it.

As for the performance issue you fear might appear, trust your compiler.

EDIT2
I forgot to mention the answer to the first part of your code : use the following trick to add or remove members depending on "feature".

namespace helper
{
  template<int feature>
  struct A;

  template<>
  struct A<0> { // add member variables for case 0 };

  template<>
  struct A<1> { // add member variables for case 1 };
}

template<int feature>
class A : private helper::A<feature>
{
  // ... functions here
};



回答2:


If you want to avoid duplicating the logic of function f you can use the template method pattern (no, not that kind of template.

template <bool enabled>
class helper {
protected:
    void foo() { /* do nothing */ }
};

template <>
class helper<true> {
protected:
    Y y;
    void foo() { /* do something with y */ }
};

struct A : private helper<WITH_FEATURE_X> {
    void f() {
        // common stuff

        foo(); // optimized away when WITH_FEATURE_X is false

        // more common stuff
    }
};



回答3:


The common solution, is just to use #ifdef I'm afraid. :-)




回答4:


I don't get the point of common code duplication. If u are using template parameter u are just replacing your #ifdef with if(WITH_FEATURE_X). Are u talking about code bloat by the compiler. Since you are trying to replace #ifdef I am assuming you would be using either A<0> or A<1> at any point. So I don't see a code bloat even by the compiler.



来源:https://stackoverflow.com/questions/3615439/template-parameters-define-and-code-duplication

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!