哪种方法更适合向函数提供编译时常量?函数参数与模板参数

发布于 12-21 23:43 字数 813 浏览 7 评论 0原文

我在整个代码的几个地方调用了日志记录函数。对于每个日志,我必须提供2 个编译时间常量。有两种方法可以完成:

(1)函数参数

template<typename T>
void log (const T &obj, const int LINE, const int COUNT)
{
  // T is used for some purpose
  if(debug)
    logging(obj.out(), LINE, COUNT);
}

将其称为,

log(str, __LINE__, __COUNTER__);

(2)模板参数

template<typename T, int LINE, int COUNT>
void log (T &obj)
{
  // T is used for some purpose
  if(debug)
    logging(obj.out(), LINE, COUNT);
}

将其称为,

log<__LINE__, __COUNTER__>(str);

我无法决定,因为第一个方法很简单,但我们在编译时传递常量。第二种方法是完美的,但编译时间可能会增加。这个任务很繁琐,而且我还没有实现任何一个,所以我没有任何基准。

如果有人可以根据他们的经验/知识来回答这个问题,那将是一个很大的帮助。

I have logging function being called at several places throughout the code. To every log, I have to supply 2 compile time constants. There are 2 approaches to accomplish:

(1) Function argument:

template<typename T>
void log (const T &obj, const int LINE, const int COUNT)
{
  // T is used for some purpose
  if(debug)
    logging(obj.out(), LINE, COUNT);
}

call it as,

log(str, __LINE__, __COUNTER__);

(2) Template parameter:

template<typename T, int LINE, int COUNT>
void log (T &obj)
{
  // T is used for some purpose
  if(debug)
    logging(obj.out(), LINE, COUNT);
}

call it as,

log<__LINE__, __COUNTER__>(str);

I am not able to decide, because 1st approach offers simplicity, but we are passing constant at compile time. 2nd approach is perfect, but compilation time would probably increase. This task is tedious, and I haven't implemented any of them yet, so I don't have any bench mark.

It will be a great help if someone can answer this from their experience/knowledge.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不喜欢何必死缠烂打2024-12-28 23:43:24

由于这两者之间的选择会对调用代码产生影响,因此我建议通过宏进行日志记录。那么您现在不必担心哪个更好,因为在它们之间切换很容易。

一旦编写了真正的应用程序,您就可以修改宏定义来比较两者。或者不,如果有更多的生产领域需要优化。如果结果证明有很大的不同,您甚至可以将其保留给构建配置,以决定是否使用 -DLOGGING_COMPILES_QUICKLY 或 -DLOGGING_RUNS_QUICKLY 。

宏的另一个潜在好处是:您可以安排当且仅当 debug 为 true 时才计算第一个参数。我不知道 str 的接口是什么,也不知道这些对象来自哪里,但如果生成正确的值传递给 log 需要付出什么代价,然后log 在非调试情况下不使用它,那么这可能会浪费运行时间。

Since the choice between these two makes a difference to the calling code, I would recommend logging via a macro. Then you don't have to worry now about which of these is better, because it's easy to switch between them.

Once you have your real application written, you can mess with the macro definition to compare the two. Or not, if there are more productive areas to optimize. If it turns out to make a big difference, you can even leave it open to the build config to decide whether to use -DLOGGING_COMPILES_QUICKLY or -DLOGGING_RUNS_QUICKLY.

Another potential benefit of a macro: you could arrange that the first argument is evaluated if and only if debug is true. I don't know what the interface of str is, or where those objects come from, but if it costs anything to produce the right value to pass to log, and then log doesn't use it in the non-debug case, then that's a potential waste of runtime.

灯下孤影2024-12-28 23:43:24

我会选择第一个选项。传递两个整数对性能的影响可以忽略不计。优化器也可能会内联函数调用,在这种情况下,两者之间没有区别。我认为第二个选项是一个坏主意,因为您将无缘无故地创建同一函数的许多版本。

I would go with the first option. The performance impact of passing two integers is negligible. The optimizer will also probably inline the function call in which case there would be no difference between the two. The second option I think is a bad idea, since you will be creating a lot of versions of the same function, for no reason.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文